SecurityState 类

定义

提供一个基类,用于从 AppDomainManager 对象中请求操作的安全状态。Provides a base class for requesting the security status of an action from the AppDomainManager object.

public ref class SecurityState abstract
public abstract class SecurityState
[System.Security.SecurityCritical]
public abstract class SecurityState
type SecurityState = class
Public MustInherit Class SecurityState
继承
SecurityState
派生
属性

示例

下面的示例演示如何重写 SecurityState 类。The following example shows how to override the SecurityState class.

[SecurityCritical]  
public enum WebcamSecurityOptions
{
    SafeAccess,  
    FullAccess  
}  
[SecurityCritical]  
public class WebcamSecurityState : SecurityState  
{  
    public WebcamSecurityState(string description, WebcamSecurityOptions options)  
    {  
        Options = options;  
        Description = description;  
    }  
    public WebcamSecurityOptions Options { get; set; }  
    public string Description { get; set; }  
    public override void EnsureState()  
    {  
        if (!IsStateAvailable())  
            throw new ApplicationException("WebcamSecurityState not available");  
    }  
}    

注解

必须从此类继承以提供一个类,该类包含一个属性,该属性指定要执行的安全敏感操作。You must inherit from this class to provide a class that includes a property that specifies the security-sensitive action to be performed. 从派生类实例化的 SecurityState 对象将传递到 AppDomainManager.CheckSecuritySettings 方法,以确定宿主是否允许执行该操作。The SecurityState object that is instantiated from the derived class is passed to the AppDomainManager.CheckSecuritySettings method to determine whether the host allows that action to be performed. CheckSecuritySettings 方法的默认实现始终返回 falseThe default implementation of the CheckSecuritySettings method always returns false. 必须重写方法以检测允许的操作。The method must be overridden to detect the actions that are allowed.

例如,如果想要在库代码中访问 Web 摄像机,则需要创建一个 WebcamSecurityState 对象,并调用 EnsureState 方法来确定该主机是否允许访问 Web 摄像机,如以下示例中所示。For example, if you want to access a Web camera in your library code, you would create a WebcamSecurityState object and call the EnsureState method to determine whether the host allows Web camera access, as shown in the following example. (您可以在 "示例" 部分中找到 WebcamSecurityState 的代码。)(You can find the code for WebcamSecurityState in the "Example" section.)

var securityState = new WebcamSecurityState("Safe access to Webcam", WebcamSecurityOptions.SafeAccess);  
try  
{  
    securityState.EnsureState();  
    // Allocate memory.  
}  
catch (ApplicationException e)  
{  
    // Handle any exception that is thrown.  
    System.Diagnostics.Debug.WriteLine(e.Message);  
}  

若要处理调用,必须通过重写 AppDomainManager.CheckSecuritySettings 方法使 AppDomainManager 了解 WebcamSecurityState,如下面的示例中所示。To process the call the AppDomainManager must be made aware of WebcamSecurityState through an override of the AppDomainManager.CheckSecuritySettings method, as shown in the following example.

[SecurityCritical]  
public override bool CheckSecuritySettings(System.Security.SecurityState securityState)  
{  
    switch (securityState.ToString())  
    {  
        case "WebcamSecurityState":  
        {  
            if ((securityState as WebcamSecurityState).Options == WebcamSecurityOptions.SafeAccess)  
                return PromptUser((securityState as WebcamSecurityState).Description);  
            return false;  
        }  
    }  
    return false;  
}  
private bool PromptUser(string text)  
{  
    // Replace the OpenFileDialog with a custom user prompt.  
    OpenFileDialog o = new OpenFileDialog();  
    o.Title = text;  
    if (o.ShowDialog() == DialogResult.OK)  
        return true;  
    else  
        return false;  
}   

构造函数

SecurityState()

初始化 SecurityState 类的新实例。Initializes a new instance of the SecurityState class.

方法

EnsureState()

在派生类中重写时,确保由 SecurityState 表示的状态在宿主上可用。When overridden in a derived class, ensures that the state that is represented by SecurityState is available on the host.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
IsStateAvailable()

获取一个值,该值指示 SecurityState 类的此实现的状态是否在当前宿主上可用。Gets a value that indicates whether the state for this implementation of the SecurityState class is available on the current host.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

适用于