This documentation is archived and is not being maintained.

Thread::CurrentPrincipal Property

Gets or sets the thread's current principal (for role-based security).

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)

public:
static property IPrincipal^ CurrentPrincipal {
	IPrincipal^ get ();
	void set (IPrincipal^ value);
}

Property Value

Type: System.Security.Principal::IPrincipal
An IPrincipal value representing the security context.

ExceptionCondition
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 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)];
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 );
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Show: