Click here to Skip to main content
15,910,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
dear all

i am using this code to let the application know if the user logged is as administrator or normal user
C#
WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
           //To show user name use
           //windowsIdentity.Name.ToString()

           WindowsPrincipal windowsPrincipal = new WindowsPrincipal(windowsIdentity);
           //To check if user is administrator use IsInRole method
           //    MessageBox.Show("Is Adminisitrator = " + windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator));

           if (windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator) == true)
           {
               admini = "1";//admisnistrator
           }
           else
           {
               admini = "2";//non-admisnistrator
           }


it is working well but the problem is when a normal user has the privileges of administrator

this code detects that he is not administrator while he has the privileges to work as admin.


i want the normal user that has the privileges to work as admin to use my application correctly but i can not

any idea
Posted

1 solution

Hi
This should work as long as you are using windows 7 or later. Some of the answers to similiar qustions suggest it might have something to do with UAC. Check out the following articles - maybe you will find a solution to your problem.

link1: http://stackoverflow.com/questions/11955302/cannot-detect-if-a-user-is-running-with-elevated-privileges-when-no-uac-popup[^]

link2: http://stackoverflow.com/questions/13934946/detect-if-running-with-elevated-privileges-domain-administrator-accounts-inclu[^]

link3: http://stackoverflow.com/questions/1220213/detect-if-running-with-elevated-privileges[^]
 
Share this answer
 
Comments
john_zakaria 20-Jun-13 7:23am    
u mean that windows xp or vista this code will not get the exact results?
koleraba 20-Jun-13 7:32am    
Ansewers on the provided links suggest that when the user has elevated priviliges the code you provided should work when running of Win7 or later. That is: Expression windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator) should return true even if the current user is not an administrator but has elevated priviliges.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900