Tuesday, November 30, 2004

WindowsIdentity, WindowsPrinciple and Role based security

Here is a small code snippet I wrote to show how to use WindowsIdentity, WindowsPrinciple objects and Role based security.


// Tells the clr which principal is in use
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);


// Gets the identity of the current user
WindowsIdentity wi = WindowsIdentity.GetCurrent();


// prints the name of the current windows user
Console.WriteLine(wi.Name);


// prints the type of the authentication
Console.WriteLine(wi.AuthenticationType);


// make an object of WindowsPrinciple
WindowsPrincipal prin = new WindowsPrincipal(wi);


// prints the name of the current windows user
Console.WriteLine(prin.Identity.Name);

// checks if the user is an Administrator return false if not else return true
// This is where we check for the role based security
Console.WriteLine(prin.IsInRole(WindowsBuiltInRole.Administrator));



No comments: