Saturday, August 23, 2008

Code: Determining If A User Is An Admin

There are several built in roles to help determine if the user is an Admin, User or Guest.


 WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();

WindowsPrincipal currentPrincipal = newWindowsPrincipal(currentIdentity);
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
if (currentPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
{
  //Administator code
}
if (currentPrincipal.IsInRole(WindowsBuiltInRole.User))
{
 // User code
}
if (currentPrincipal.IsInRole(WindowsBuiltInRole.Guest))
{
 // Guest or anonymous code
}

No comments: