How to Use CurrentPrincipal

The user running a .Net application is identified by the Thread.CurrentPrincipal property of the running thread. You can set the CurrentPrincipal at application startup as follows.

string[] roles = { "Manager", "Administrator" };
Thread.CurrentPrincipal = new GenericPrincipal(
new GenericIdentity("John"), roles);

Then later you can make authorization decisions based on the current user.

if (!System.Threading.Thread.CurrentPrincipal.IsInRole("Manager"))
{
    Console.WriteLine("Permission denied");
    return;
}
else{
    Console.WriteLine("Permission granted");
    return;
}

References

msdn documentation.

Comments