PrincipalPermissionAttribute Constructor (SecurityAction)
Initializes a new instance of the PrincipalPermissionAttribute class with the specified SecurityAction.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- 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.
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; [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; }
Available since 1.1
