Edytuj

Udostępnij za pośrednictwem


IApplicationTrustManager.DetermineApplicationTrust Method

Definition

Determines whether an application should be executed and which set of permissions should be granted to it.

public:
 System::Security::Policy::ApplicationTrust ^ DetermineApplicationTrust(ActivationContext ^ activationContext, System::Security::Policy::TrustManagerContext ^ context);
public System.Security.Policy.ApplicationTrust DetermineApplicationTrust (ActivationContext activationContext, System.Security.Policy.TrustManagerContext context);
abstract member DetermineApplicationTrust : ActivationContext * System.Security.Policy.TrustManagerContext -> System.Security.Policy.ApplicationTrust
Public Function DetermineApplicationTrust (activationContext As ActivationContext, context As TrustManagerContext) As ApplicationTrust

Parameters

activationContext
ActivationContext

The activation context for the application.

context
TrustManagerContext

The trust manager context for the application.

Returns

An object that contains security decisions about the application.

Examples

The following example shows an implementation of the DetermineApplicationTrust method for a custom trust manager. This code example is part of a larger example provided for the IApplicationTrustManager interface.

public ApplicationTrust DetermineApplicationTrust(ActivationContext appContext, TrustManagerContext context)
{
    ApplicationTrust trust = new ApplicationTrust(appContext.Identity);
    trust.IsApplicationTrustedToRun = false;

    ApplicationSecurityInfo asi = new ApplicationSecurityInfo(appContext);
    trust.DefaultGrantSet = new PolicyStatement(asi.DefaultRequestSet, PolicyStatementAttribute.Nothing);
    if (context.UIContext == TrustManagerUIContext.Run)
    {
        string message = "Do you want to run " + asi.ApplicationId.Name + " ?";
        string caption = "MyTrustManager";
        MessageBoxButtons buttons = MessageBoxButtons.YesNo;
        DialogResult result;

        // Displays the MessageBox.

        result = MessageBox.Show(message, caption, buttons);

        if (result == DialogResult.Yes)
        {
            trust.IsApplicationTrustedToRun = true;
            if (context != null)
                trust.Persist = context.Persist;
            else
                trust.Persist = false;
        }
    }

    return trust;
}
Public Function DetermineApplicationTrust(ByVal appContext As ActivationContext, ByVal context As TrustManagerContext) As ApplicationTrust Implements IApplicationTrustManager.DetermineApplicationTrust
    Dim trust As New ApplicationTrust(appContext.Identity)
    trust.IsApplicationTrustedToRun = False

    Dim asi As New ApplicationSecurityInfo(appContext)
    trust.DefaultGrantSet = New PolicyStatement(asi.DefaultRequestSet, _
    PolicyStatementAttribute.Nothing)
    If context.UIContext = TrustManagerUIContext.Run Then
        Dim message As String = "Do you want to run " + asi.ApplicationId.Name + " ?"
        Dim caption As String = "MyTrustManager"
        Dim buttons As MessageBoxButtons = MessageBoxButtons.YesNo
        Dim result As DialogResult

        ' Displays the MessageBox.
        result = MessageBox.Show(message, caption, buttons)

        If result = DialogResult.Yes Then
            trust.IsApplicationTrustedToRun = True
            If Not (context Is Nothing) Then
                trust.Persist = context.Persist
            Else
                trust.Persist = False
            End If
        End If
    End If
    Return trust

End Function 'DetermineApplicationTrust

Remarks

The DetermineApplicationTrust method is called by the host to determine whether an application should be executed and which set of permissions it should be granted. DetermineApplicationTrust returns an ApplicationTrust object with a DefaultGrantSet property that contains a permission set representing the permissions to be granted to each assembly executing within the context of the application. The granted permissions do not apply to assemblies in the global assembly cache. The ApplicationTrust object also has an IsApplicationTrustedToRun property that the trust manager sets to indicate whether the application should be trusted. If the trust manager indicates that the application can be trusted, the host activates the application and grants its assemblies the set of permissions provided in the ApplicationTrust collection.

Applies to