Determining if the user has admin privileges

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I'm writing a C# .NET code in which I need to first determine if the
user has administrative privlieges on that system (or if the user belongs to
the administrative group). Could anyone please tell me how I can achieve this.

Thank you

Ravi.
 
You need to use the WindowsPrinciple class in the System.Security.Principal
namespace.

e.g.
=====================
using System.Security.Principal;
using System.Threading;

....
....

Thread.GetDomain().SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsPrincipal wp = (WindowsPrincipal)Thread.CurrentPrincipal;
bool isAdmin = wp.IsInRole(WindowsBuiltInRole.Administrator);
Console.WriteLine(isAdmin);
=====================

The above code will print out "True" if the current user is an Administrator
and "False" if not;

Hope this helps!

Brian Delahunty
Ireland

http://briandela.com/blog
 
Hi Brian,
Thanks for the quick reply to my question. Your suggestion is
very helpful to me at this time.

Thank you

Ravi.
 
Back
Top