How to: Create a WindowsPrincipal Object
There are two ways to create aobject, depending on whether code must repeatedly perform role-based validation or must perform it only once.
If code must repeatedly perform role-based validation, the first of the following procedures produces less overhead. When code needs to make role-based validations only once, you can create a WindowsPrincipal object by using the second of the following procedures.
To create a WindowsPrincipal object for repeated validation
Call themethod on the object that is returned by the static property, passing the method a enumeration value that indicates what the new policy should be. Supported values are , , and . The following code demonstrates this method call.
With the policy set, use the staticproperty to retrieve the principal that encapsulates the current Windows user. Because the property return type is , you must cast the result to a WindowsPrincipal type. The following code initializes a new WindowsPrincipal object to the value of the principal associated with the current thread.
To create a WindowsPrincipal object for a single validation
Initialize a newobject by calling the static method, which queries the current Windows account and places information about that account into the newly created identity object. The following code creates a new WindowsIdentity object and initializes it to the current authenticated user.
Create a new WindowsPrincipal object and pass it the value of the WindowsIdentity object created in the preceding step.
When the principal object has been created, you can use one of several methods to validate it. For more information, see.