CurrentPrincipal Property

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 IPrincipal CurrentPrincipal {
	get;
	[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlPrincipal)]
	set;
}

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 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.

.NET Framework
Available since 1.1
Return to top
Show:
© 2016 Microsoft