Live with Dot Net Just another Programming weblog

Check whether the current user have the admin rights

Posted on August 1, 2008

You can check the current user privilege using the identity principal of the current user. Below code helps you to check whether the current user has the Administrator privilege or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public bool CheckAdminRights()
        {
            bool IsAdmin;
            try
            {
                AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
                WindowsPrincipal principal = (WindowsPrincipal)Thread.CurrentPrincipal;
                WindowsIdentity identity = (WindowsIdentity)principal.Identity;
                //check whether the current user principal have admin rights
                IsAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
                Console.WriteLine("Identity Type: " + identity.ToString());
                Console.WriteLine("Name: " + identity.Name);
                Console.WriteLine("'Users'?: " + principal.IsInRole("adminsys\\users"));
                Console.WriteLine("'Administrators'?:  " + principal.IsInRole(WindowsBuiltInRole.Administrator));
                Console.WriteLine("Authenticated: " + identity.IsAuthenticated);
                Console.WriteLine("AuthType: " + identity.AuthenticationType);
                Console.WriteLine("Anonymous? " + identity.IsAnonymous);
                Console.WriteLine("Token: " + identity.Token);
                Console.ReadLine();
                return IsAdmin;
            }
            catch (Exception Ex)  { return false;  }
        }

About admin

No description. Please complete your profile.
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.