.NET Framework Class Library
Thread.CurrentPrincipal Property
Gets or sets the thread's current principal (for role-based security).
Assembly: mscorlib (in mscorlib.dll)
Syntax
Visual Basic
Public Shared Property CurrentPrincipal As IPrincipal
C#
public static IPrincipal CurrentPrincipal { get; set; }
Visual C++
public: static property IPrincipal^ CurrentPrincipal { IPrincipal^ get (); void set (IPrincipal^ value); }
F#
static member CurrentPrincipal : IPrincipal with get, set
Property Value
Type: System.Security.Principal.IPrincipalAn IPrincipal value representing the security context.
Exceptions
| Exception | Condition |
|---|---|
| SecurityException |
The caller does not have the permission required to set the principal. |
Examples
The following code example shows how to set and retrieve the principal of a thread.
Visual Basic
Option Explicit Option Strict Imports Microsoft.VisualBasic Imports System Imports System.Security Imports System.Security.Permissions Imports System.Security.Principal Imports System.Threading Public Class Principal <MTAThread> _ Shared Sub Main() Dim rolesArray As String() = {"managers", "executives"} Try ' Set the principal to a new generic principal. Thread.CurrentPrincipal = _ New GenericPrincipal(New GenericIdentity( _ "Bob", "Passport"), rolesArray) Catch secureException As SecurityException Console.WriteLine("{0}: Permission to set Principal " & _ "is denied.", secureException.GetType().Name) End Try Dim threadPrincipal As IPrincipal = Thread.CurrentPrincipal Console.WriteLine( _ "Name: {0}" & vbCrLf & "IsAuthenticated:" & _ " {1}" & vbCrLf & "AuthenticationType: {2}", _ threadPrincipal.Identity.Name, _ threadPrincipal.Identity.IsAuthenticated, _ threadPrincipal.Identity.AuthenticationType) End Sub End Class
C#
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); } }
Visual C++
using namespace System; using namespace System::Security; using namespace System::Security::Permissions; using namespace System::Security::Principal; using namespace System::Threading; int main() { array<String^>^rolesArray = {"managers","executives"}; try { // Set the principal to a new generic principal. Thread::CurrentPrincipal = gcnew GenericPrincipal( gcnew 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.ToString(), threadPrincipal->Identity->AuthenticationType ); }
Version Information
.NET Framework
Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0.NET Framework Client Profile
Supported in: 4, 3.5 SP1.NET Framework Security
-
SecurityPermission
to manipulate the principal object. Associated enumeration: SecurityPermissionFlag.ControlPrincipal.
Platforms
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
See Also