In some cases in your windows application you may want to know is the current user a member of Aministrators group or not?
To detect this you can get an object of WindowsIdentity like this:
WindowsIdentity identity = WindowsIdentity.GetCurrent();
Then create an instance of WindowsPrincipan by :
WindowsPrincipal principal = new WindowsPrincipal(identity);
and finally check it by using IsInRole() method like this:
string role = "BUILTIN\\Administrators";
bool IsAdmin = principal.IsInRole(role));
then you can use the IsAdmin variable to determine whether the current user is an Admin or not.
Saturday, September 8, 2007
Detecting is current user an Administrator
Subscribe to:
Post Comments (Atom)
5 comments:
you can also do this:
bool IsAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
in Vista IsAdmin always is false.
any idea?
In Vista they have to be elevated to administrator. See http://visualstudiomagazine.com/columns/article.aspx?editorialsid=2337 for details.
thank you jared, this will help me.
I tried this with domain user that's added to local administrators group but it still cannot return true.
Any idea?
Post a Comment