Users

  • Thread starter Thread starter Alberto
  • Start date Start date
Alberto,

First, you will have to get the current windows user identity. You can
do this by calling the static GetCurrent method on the WindowsIdentity
class. Then, pass that WindowsIdentity instance to the constructor of a
WindowsPrincipal class. With that, you can call the IsInRole method and
pass the Administrators role (a string), or you can pass in the
Administrator value from the WindowsBuiltInRole enumeration.

Hope this helps.
 
Watch for word wrap but this should do it:

WindowsPrincipal myPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;
if (myPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
{
...
}

Have A Better One!

John M Deal, MCP
Necessity Software
 
One thing to note here is that if you do use the string parameter option
(as opposed to the enum) you need to actually pass
"BUILTIN\Administrators" or it will not recognize it. This is true for
all of the roles in the WindowsBuiltInRole. I mention this because it
had me going for a bit when I needed it and thought I could just pass
the "Administrators" in. Just thought I'd mention it.

Have A Better One!

John M Deal, MCP
Necessity Software
 
While you could use the CurrentPrincipal on the thread, it's not
guaranteed that it will always return a WindowsPrincipal, so you have to
watch out for it.
 
Good point, I got the code I posted out of the MSDN documentation. This
is the line I use in my code when I do this kind of thing:

WindowsPrincipal wp = new
WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent());

I'm pretty sure this one is solid (but let me know if I'm wrong about
it, thanks).

John Deal, MCP
Necessity Software
 
Back
Top