PrincipalPermission Constructors

Definition

Initializes a new instance of the PrincipalPermission class.

Overloads

PrincipalPermission(PermissionState)

Initializes a new instance of the PrincipalPermission class with the specified PermissionState.

PrincipalPermission(String, String)

Initializes a new instance of the PrincipalPermission class for the specified name and role.

PrincipalPermission(String, String, Boolean)

Initializes a new instance of the PrincipalPermission class for the specified name, role, and authentication status.

PrincipalPermission(PermissionState)

Source:
PrincipalPermission.cs
Source:
PrincipalPermission.cs
Source:
PrincipalPermission.cs
Source:
PrincipalPermission.cs
Source:
PrincipalPermission.cs

Initializes a new instance of the PrincipalPermission class with the specified PermissionState.

C#
public PrincipalPermission(System.Security.Permissions.PermissionState state);

Parameters

state
PermissionState

One of the PermissionState values.

Exceptions

The state parameter is not a valid PermissionState.

Remarks

None matches only the unauthenticated principal (Name is the empty string (""), no Role, Authenticated is false). Unrestricted matches all principals (Name is null, Role is null).

Note

This constructor is included for consistency with the design of other permissions, but is not useful in practice.

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

PrincipalPermission(String, String)

Source:
PrincipalPermission.cs
Source:
PrincipalPermission.cs
Source:
PrincipalPermission.cs
Source:
PrincipalPermission.cs
Source:
PrincipalPermission.cs

Initializes a new instance of the PrincipalPermission class for the specified name and role.

C#
public PrincipalPermission(string name, string role);

Parameters

name
String

The name of the IPrincipal object's user.

role
String

The role of the IPrincipal object's user (for example, Administrator).

Examples

The following example requires the active principal to be an administrator. The name parameter is null, which enables any user who is an administrator to pass the demand.

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.

C#
using System;
using System.Threading;
using System.Security.Permissions;
using System.Security.Principal;

class SecurityPrincipalDemo
{

    public static void Main()
    {
        AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
        PrincipalPermission principalPerm = new PrincipalPermission(null, "Administrators");
        principalPerm.Demand();
        Console.WriteLine("Demand succeeded.");
    }
}

Remarks

Both the name parameter and the role parameter must match for this permission to match the active IPrincipal and associated IIdentity. Set name to null to check for any user in a role.

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

PrincipalPermission(String, String, Boolean)

Source:
PrincipalPermission.cs
Source:
PrincipalPermission.cs
Source:
PrincipalPermission.cs
Source:
PrincipalPermission.cs
Source:
PrincipalPermission.cs

Initializes a new instance of the PrincipalPermission class for the specified name, role, and authentication status.

C#
public PrincipalPermission(string name, string role, bool isAuthenticated);

Parameters

name
String

The name of the IPrincipal object's user.

role
String

The role of the IPrincipal object's user (for example, Administrator).

isAuthenticated
Boolean

true to signify that the user is authenticated; otherwise, false.

Remarks

Both the name parameter and the role parameter must match for this permission to match the active IPrincipal and associated IIdentity.

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10