This documentation is archived and is not being maintained.
Thread.CurrentPrincipal Property
.NET Framework 1.1
Gets or sets the thread's current principal (for role-based security).
[Visual Basic] Public Shared Property CurrentPrincipal As IPrincipal [C#] public static IPrincipal CurrentPrincipal {get; set;} [C++] public: __property static IPrincipal* get_CurrentPrincipal(); public: __property static void set_CurrentPrincipal(IPrincipal*); [JScript] public static function get CurrentPrincipal() : IPrincipal; public static function set CurrentPrincipal(IPrincipal);
Property Value
An IPrincipal value representing the security context.
Exceptions
| Exception Type | Condition |
|---|---|
| SecurityException | The caller does not have the required permission. |
Example
[Visual Basic, C#, C++] 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 ' Request permission to set thread principal. <Assembly: SecurityPermissionAttribute( _ SecurityAction.RequestOptional, ControlPrincipal := True)> Public Class Principal 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; // Request permission to set thread principal. [assembly: SecurityPermissionAttribute( SecurityAction.RequestOptional, ControlPrincipal = true)] 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); } } [C++] #using <mscorlib.dll> using namespace System; using namespace System::Security; using namespace System::Security::Permissions; using namespace System::Security::Principal; using namespace System::Threading; // Request permission to set thread principal. [assembly: SecurityPermissionAttribute( SecurityAction::RequestOptional, ControlPrincipal = true)]; [assembly: SecurityPermissionAttribute( SecurityAction::RequestMinimum, UnmanagedCode = true)]; void main() { String* rolesArray[] = {S"managers", S"executives"}; try { // Set the principal to a new generic principal. Thread::CurrentPrincipal = new GenericPrincipal(new GenericIdentity( S"Bob", S"Passport"), rolesArray); } catch(SecurityException* secureException) { Console::WriteLine(S"{0}: Permission to set Principal " S"is denied.", secureException->GetType()->Name); } IPrincipal* threadPrincipal = Thread::CurrentPrincipal; Console::WriteLine(S"Name: {0}\nIsAuthenticated: {1}" S"\nAuthenticationType: {2}", threadPrincipal->Identity->Name, threadPrincipal->Identity->IsAuthenticated.ToString(), threadPrincipal->Identity->AuthenticationType); }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
.NET Framework Security:
- SecurityPermission to manipulate the principal object. Associated enumeration: SecurityPermissionFlag.ControlPrincipal.
See Also
Show: