Thread::CurrentPrincipal Property

 

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

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

public:
property IPrincipal^ CurrentPrincipal {
	static IPrincipal^ get();
	[SecurityPermissionAttribute(SecurityAction::Demand, Flags = SecurityPermissionFlag::ControlPrincipal)]
	static void set(IPrincipal^ value);
}

Property Value

Type: System.Security.Principal::IPrincipal^

An 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 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 );
}

SecurityPermission

to manipulate the principal object. Associated enumeration: SecurityPermissionFlag::ControlPrincipal.

.NET Framework
Available since 1.1
Return to top
Show: