PrincipalPermissionAttribute(SecurityAction) 构造函数

定义

注意

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

使用指定的 PrincipalPermissionAttribute 初始化 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)

参数

action
SecurityAction

SecurityAction 值之一。

属性

示例

以下示例演示如何使用 PrincipalPermissionAttribute 构造函数来要求当前用户是管理员。

注意

在 Windows Vista 中,用户帐户控制 (UAC) 决定用户的特权。 如果您是内置的 Administrators 组的成员,将为您分配两个运行时访问令牌:一个标准用户访问令牌和一个管理员访问令牌。 默认情况下,您拥有标准用户角色。 要执行需要管理员身份的代码,必须首先将你的特权从标准用户提升至管理员。 你可以通过以下方式执行此操作:右键单击应用程序图标并指示需以管理员身份运行。

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

注解

DemandInheritanceDemandLinkDemand 是唯一对此属性有意义的值 SecurityAction 。 其他操作不适用于不是代码访问权限的权限。

适用于