Redigera

Dela via


PrincipalPermissionAttribute(SecurityAction) Constructor

Definition

Caution

PrincipalPermissionAttribute is not honored by the runtime and must not be used.

Initializes a new instance of the PrincipalPermissionAttribute class with the specified SecurityAction.

public:
 PrincipalPermissionAttribute(System::Security::Permissions::SecurityAction action);
[System.Obsolete("PrincipalPermissionAttribute is not honored by the runtime and must not be used.", true, DiagnosticId="SYSLIB0002", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public PrincipalPermissionAttribute (System.Security.Permissions.SecurityAction action);
public PrincipalPermissionAttribute (System.Security.Permissions.SecurityAction action);
[<System.Obsolete("PrincipalPermissionAttribute is not honored by the runtime and must not be used.", true, DiagnosticId="SYSLIB0002", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
new System.Security.Permissions.PrincipalPermissionAttribute : System.Security.Permissions.SecurityAction -> System.Security.Permissions.PrincipalPermissionAttribute
new System.Security.Permissions.PrincipalPermissionAttribute : System.Security.Permissions.SecurityAction -> System.Security.Permissions.PrincipalPermissionAttribute
Public Sub New (action As SecurityAction)

Parameters

action
SecurityAction

One of the SecurityAction values.

Attributes

Examples

The following example demonstrates how to use the PrincipalPermissionAttribute constructor to demand that the current user be an administrator.

Note

In Windows Vista, User Account Control (UAC) determines the privileges of a user. If you are a member of the Built-in Administrators group, you are assigned two run-time access tokens: a standard user access token and an administrator access token. By default, you are in the standard user role. To execute the code that requires you to be an administrator, you must first elevate your privileges from standard user to administrator. You can do this when you start an application by right-clicking the application icon and indicating that you want to run as an administrator.

using namespace System;
using namespace System::Security;
using namespace System::Security::Permissions;
using namespace System::Security::Policy;
using namespace System::Security::Principal;

[PrincipalPermission(SecurityAction::Demand, Role = "Administrators")]
void CheckAdministrator()
{
    Console::WriteLine("User is an administrator.");
}
int main(array<System::String ^> ^args)
{
    try
    {
        // Must set PrincipalPolicy to WindowsPrincipal
        AppDomain::CurrentDomain->SetPrincipalPolicy(PrincipalPolicy::WindowsPrincipal);
        // Check using declarative security.
        CheckAdministrator();
        // Check using Imperative security.
        System::String^ null;
        PrincipalPermission^ principalPerm = gcnew PrincipalPermission(null, "Administrators" );
        principalPerm->Demand();
        Console::WriteLine("Demand succeeded");
    }
    catch (Exception ^e)
    {
        Console::WriteLine(e->Message);
    }
    return 0;
}
using System;
using System.Threading;
using System.Security.Permissions;
using System.Security.Principal;

class SecurityPrincipalDemo
{
    public static void Main()
    {
        try
        {
            // PrincipalPolicy must be set to WindowsPrincipal to check roles.
            AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
            // Check using the PrincipalPermissionAttribute
            CheckAdministrator();
            // Check using PrincipalPermission class.
            PrincipalPermission principalPerm = new PrincipalPermission(null, "Administrators");
            principalPerm.Demand();
            Console.WriteLine("Demand succeeded.");
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
    [PrincipalPermission(SecurityAction.Demand, Role = "Administrators")]
    static void CheckAdministrator()
    {
        Console.WriteLine("User is an administrator");
    }
}
Imports System.Threading
Imports System.Security.Permissions
Imports System.Security.Principal



Class SecurityPrincipalDemo

    Public Shared Sub Main()
        Try
            ' PrincipalPolicy must be set to WindowsPrincipal to check roles.
            AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
            ' Check using the PrincipalPermissionAttribute
            CheckAdministrator()
            ' Check using PrincipalPermission class.
            Dim principalPerm As New PrincipalPermission(Nothing, "Administrators")
            principalPerm.Demand()
            Console.WriteLine("Demand succeeded.")
        Catch e As Exception
            Console.WriteLine(e.Message)
        End Try

    End Sub

    <PrincipalPermission(SecurityAction.Demand, Role:="Administrators")> _
    Shared Sub CheckAdministrator()
        Console.WriteLine("User is an administrator")

    End Sub
End Class

Remarks

Demand, InheritanceDemand, and LinkDemand are the only values of SecurityAction that have meaning for this attribute. Other actions do not apply to permissions that are not code access permissions.

Applies to