Thread.CurrentPrincipal Property
.NET Framework 4.5
Gets or sets the thread's current principal (for role-based security).
Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)
Property Value
Type: System.Security.Principal.IPrincipalAn IPrincipal value representing the security context.
| Exception | Condition |
|---|---|
| SecurityException | The caller does not have the permission required to set the principal. |
The following code example shows how to set and retrieve the principal of a thread.
using System; using System.Security; using System.Security.Permissions; using System.Security.Principal; using System.Threading; class Principal { static void Main() { string[] rolesArray = {"managers", "executives"}; try { // Set the principal to a new generic principal. Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity( "Bob", "Passport"), rolesArray); } catch(SecurityException secureException) { Console.WriteLine("{0}: Permission to set Principal " + "is denied.", secureException.GetType().Name); } IPrincipal threadPrincipal = Thread.CurrentPrincipal; Console.WriteLine("Name: {0}\nIsAuthenticated: {1}" + "\nAuthenticationType: {2}", threadPrincipal.Identity.Name, threadPrincipal.Identity.IsAuthenticated, threadPrincipal.Identity.AuthenticationType); } }
- SecurityPermission
to manipulate the principal object. Associated enumeration: SecurityPermissionFlag.ControlPrincipal.
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.