Muokkaa

Jaa


PrincipalPermission Class

Definition

Caution

Code Access Security is not supported or honored by the runtime.

Allows checks against the active principal (see IPrincipal) using the language constructs defined for both declarative and imperative security actions. This class cannot be inherited.

public ref class PrincipalPermission sealed : System::Security::IPermission, System::Security::Permissions::IUnrestrictedPermission
[System.Obsolete("Code Access Security is not supported or honored by the runtime.", DiagnosticId="SYSLIB0003", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public sealed class PrincipalPermission : System.Security.IPermission, System.Security.Permissions.IUnrestrictedPermission
[System.Serializable]
public sealed class PrincipalPermission : System.Security.IPermission, System.Security.Permissions.IUnrestrictedPermission
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class PrincipalPermission : System.Security.IPermission, System.Security.Permissions.IUnrestrictedPermission
public sealed class PrincipalPermission : System.Security.IPermission, System.Security.Permissions.IUnrestrictedPermission
[<System.Obsolete("Code Access Security is not supported or honored by the runtime.", DiagnosticId="SYSLIB0003", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
type PrincipalPermission = class
    interface IPermission
    interface ISecurityEncodable
    interface IUnrestrictedPermission
[<System.Serializable>]
type PrincipalPermission = class
    interface IPermission
    interface IUnrestrictedPermission
    interface ISecurityEncodable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type PrincipalPermission = class
    interface IPermission
    interface IUnrestrictedPermission
    interface ISecurityEncodable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type PrincipalPermission = class
    interface IPermission
    interface ISecurityEncodable
    interface IUnrestrictedPermission
type PrincipalPermission = class
    interface IPermission
    interface ISecurityEncodable
    interface IUnrestrictedPermission
Public NotInheritable Class PrincipalPermission
Implements IPermission, IUnrestrictedPermission
Inheritance
PrincipalPermission
Attributes
Implements

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.

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

int main(array<System::String ^> ^args)
{
    System::String^ null;
    AppDomain::CurrentDomain->SetPrincipalPolicy(PrincipalPolicy::WindowsPrincipal);
    PrincipalPermission^ principalPerm = gcnew PrincipalPermission(null, "Administrators" );
      principalPerm->Demand();
      Console::WriteLine("Demand succeeded");
    return 0;
}
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.");
    }
}
Imports System.Threading
Imports System.Security.Permissions
Imports System.Security.Principal



Class SecurityPrincipalDemo


    Public Shared Sub Main()
        AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
        Dim principalPerm As New PrincipalPermission(Nothing, "Administrators")
        principalPerm.Demand()
        Console.WriteLine("Demand succeeded.")

    End Sub
End Class

Remarks

Caution

Code Access Security (CAS) has been deprecated across all versions of .NET Framework and .NET. Recent versions of .NET do not honor CAS annotations and produce errors if CAS-related APIs are used. Developers should seek alternative means of accomplishing security tasks.

By passing identity information (user name and role) to the constructor, PrincipalPermission can be used to demand that the identity of the active principal matches this information.

To match the active IPrincipal and associated IIdentity, both the specified identity and role must match. If null identity string is used, it is interpreted as a request to match any identity. Use of null role string will match any role. By implication, passing null parameter for name or role to PrincipalPermission will match the identity and roles in any IPrincipal. It is also possible to construct a PrincipalPermission that only determines whether the IIdentity represents an authenticated or unauthenticated entity. In this case, name and role are ignored.

Unlike most other permissions, PrincipalPermission does not extend CodeAccessPermission. It does, however, implement the IPermission interface. This is because PrincipalPermission is not a code access permission; that is, it is not granted based on the identity of the executing assembly. Instead, it allows code to perform actions (Demand, Union, Intersect, and so on) against the current user identity in a manner consistent with the way those actions are performed for code access and code identity permissions.

Important

Prior to a demand for principal permission it is necessary to set the current application domain's principal policy to the enumeration value WindowsPrincipal. By default, the principal policy is set to UnauthenticatedPrincipal. If you do not set the principal policy to WindowsPrincipal, a demand for principal permission will fail. The following code should be executed before the principal permission is demanded:

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal).

Constructors

PrincipalPermission(PermissionState)

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

PrincipalPermission(String, String, Boolean)

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

PrincipalPermission(String, String)

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

Methods

Copy()

Creates and returns an identical copy of the current permission.

Demand()

Determines at run time whether the current principal matches the principal specified by the current permission.

Equals(Object)

Determines whether the specified PrincipalPermission object is equal to the current PrincipalPermission.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
FromXml(SecurityElement)

Reconstructs a permission with a specified state from an XML encoding.

GetHashCode()

Gets a hash code for the PrincipalPermission object that is suitable for use in hashing algorithms and data structures such as a hash table.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
Intersect(IPermission)

Creates and returns a permission that is the intersection of the current permission and the specified permission.

IsSubsetOf(IPermission)

Determines whether the current permission is a subset of the specified permission.

IsUnrestricted()

Returns a value indicating whether the current permission is unrestricted.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Creates and returns a string representing the current permission.

ToXml()

Creates an XML encoding of the permission and its current state.

Union(IPermission)

Creates a permission that is the union of the current permission and the specified permission.

Applies to

See also