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
[<System.Security.SecurityCritical>]
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.

例如,如果您想要在程式庫程式碼中存取網路攝影機,您會建立一個 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 必須透過方法的覆 WebcamSecurityState 寫來感知 AppDomainManager.CheckSecuritySettings ,如下列範例所示。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()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於