Wednesday, September 12, 2012

Check If Current User Has Specific Role (e.g. Administrator)

An easy solution of checking whether the current user has specific roles (as administrator or guest), is the following (the example checks, whether the user is guest):

WindowsIdentity CurrentIdentity = WindowsIdentity.GetCurrent();
WindowsPrincipal CurrentPrincipal = new WindowsPrincipal(CurrentIdentity);
bool UserIsAdmin = CurrentPrincipal.IsInRole(WindowsBuiltInRole.Guest);

From Windows Vista on though we get a problem, if the to be tested role is administrator. Although the user running the program might be administrator, the application is at first hand run without administrative rights, so that wrongly false is returned. Only when explicitely running the application as administrator true is returned, of course also then, if the user is no administrator.

I did not find an (easy) solution solving this problem, maybe some good advices come from you?

No comments:

Post a Comment