PrincipalPermissionAttribute Constructor (SecurityAction)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

Namespace:   System.Security.Permissions
Assembly:  mscorlib (in mscorlib.dll)

public PrincipalPermissionAttribute(
	SecurityAction action


Type: System.Security.Permissions.SecurityAction

One of the SecurityAction values.

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.

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


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 System;
using System.Threading;
using System.Security.Permissions;
using System.Security.Principal;

class SecurityPrincipalDemo
    public static void Main()
            // PrincipalPolicy must be set to WindowsPrincipal to check roles.
            // Check using the PrincipalPermissionAttribute
            // Check using PrincipalPermission class.
            PrincipalPermission principalPerm = new PrincipalPermission(null, "Administrators");
            Console.WriteLine("Demand succeeded.");
        catch (Exception e)
    [PrincipalPermission(SecurityAction.Demand, Role = "Administrators")]
    static void CheckAdministrator()
        Console.WriteLine("User is an administrator");

.NET Framework
Available since 1.1
Return to top