PrincipalPermissionAttribute(SecurityAction) PrincipalPermissionAttribute(SecurityAction) PrincipalPermissionAttribute(SecurityAction) PrincipalPermissionAttribute(SecurityAction) Constructor

定义

使用指定的 PrincipalPermissionAttribute 初始化 SecurityAction 类的新实例。Initializes a new instance of the PrincipalPermissionAttribute class with the specified SecurityAction.

public:
 PrincipalPermissionAttribute(System::Security::Permissions::SecurityAction action);
public PrincipalPermissionAttribute (System.Security.Permissions.SecurityAction action);
new System.Security.Permissions.PrincipalPermissionAttribute : System.Security.Permissions.SecurityAction -> System.Security.Permissions.PrincipalPermissionAttribute
Public Sub New (action As SecurityAction)

参数

示例

下面的示例演示如何使用PrincipalPermissionAttribute构造函数,以要求当前用户是管理员。The following example demonstrates how to use the PrincipalPermissionAttribute constructor to demand that the current user be an administrator.

备注

在 Windows Vista 中,用户帐户控制 (UAC) 决定用户的特权。In Windows Vista, User Account Control (UAC) determines the privileges of a user. 如果您是内置的 Administrators 组的成员,将为您分配两个运行时访问令牌:一个标准用户访问令牌和一个管理员访问令牌。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
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 'Main

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

    End Sub 'CheckAdministrator
End Class 'SecurityPrincipalDemo

注解

Demand``InheritanceDemand,并LinkDemand是唯一的值SecurityAction具有此特性的含义。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.

适用于