| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | dev_langs | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
How to: Create a WindowsPrincipal Object |
03/30/2017 |
.net |
dotnet-standard |
article |
|
|
56eb10ca-e61d-4ed2-af7a-555fc4c25a25 |
14 |
mairaw |
mairaw |
wpickett |
How to: Create a WindowsPrincipal Object
There are two ways to create a xref:System.Security.Principal.WindowsPrincipal object, 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 xref:System.Security.Principal.WindowsPrincipal object by using the second of the following procedures.
To create a WindowsPrincipal object for repeated validation
-
Call the xref:System.AppDomain.SetPrincipalPolicy%2A method on the xref:System.AppDomain object that is returned by the static xref:System.AppDomain.CurrentDomain%2A?displayProperty=nameWithType property, passing the method a xref:System.Security.Principal.PrincipalPolicy enumeration value that indicates what the new policy should be. Supported values are xref:System.Security.Principal.PrincipalPolicy.NoPrincipal, xref:System.Security.Principal.PrincipalPolicy.UnauthenticatedPrincipal, and xref:System.Security.Principal.PrincipalPolicy.WindowsPrincipal. The following code demonstrates this method call.
AppDomain.CurrentDomain.SetPrincipalPolicy( PrincipalPolicy.WindowsPrincipal);AppDomain.CurrentDomain.SetPrincipalPolicy( _ PrincipalPolicy.WindowsPrincipal)
-
With the policy set, use the static xref:System.Threading.Thread.CurrentPrincipal%2A?displayProperty=nameWithType property to retrieve the principal that encapsulates the current Windows user. Because the property return type is xref:System.Security.Principal.IPrincipal, you must cast the result to a xref:System.Security.Principal.WindowsPrincipal type. The following code initializes a new xref:System.Security.Principal.WindowsPrincipal object to the value of the principal associated with the current thread.
WindowsPrincipal MyPrincipal = (WindowsPrincipal) Thread.CurrentPrincipal;
Dim MyPrincipal As WindowsPrincipal = _ CType(Thread.CurrentPrincipal, WindowsPrincipal)
-
When the principal object has been created, you can use one of several methods to validate it.
To create a WindowsPrincipal object for a single validation
-
Initialize a new xref:System.Security.Principal.WindowsIdentity object by calling the static xref:System.Security.Principal.WindowsIdentity.GetCurrent%2A?displayProperty=nameWithType 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 xref:System.Security.Principal.WindowsIdentity object and initializes it to the current authenticated user.
WindowsIdentity MyIdentity = WindowsIdentity.GetCurrent();
Dim MyIdentity As WindowsIdentity = WindowsIdentity.GetCurrent()
-
Create a new xref:System.Security.Principal.WindowsPrincipal object and pass it the value of the xref:System.Security.Principal.WindowsIdentity object created in the preceding step.
WindowsPrincipal MyPrincipal = new WindowsPrincipal(MyIdentity);
Dim MyPrincipal As New WindowsPrincipal(MyIdentity)
-
When the principal object has been created, you can use one of several methods to validate it.