Why does WindowsPrincipal.IsInRole always return false for the Administrators group?

My local user account is in the Administrators group, and I just wanted to find out how the project will look like windows if I'm in the administrators group. So, I started the windows forms project and tried the following:

[STAThread] static void Main() { string adminGroup1 = @"BUILTIN\Administrators"; string adminGroup2 = Environment.MachineName + @"\Administrators"; string adminGroup3 = Environment.MachineName.ToLower() + @"\Administrators"; string adminGroup4 = "Administrators"; string adminGroup5 = "administrators"; AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); WindowsPrincipal currentUser1 = (WindowsPrincipal)Thread.CurrentPrincipal; bool IsAdmin1_1 = currentUser1.IsInRole(adminGroup1); // false bool IsAdmin1_2 = currentUser1.IsInRole(adminGroup2); // false bool IsAdmin1_3 = currentUser1.IsInRole(adminGroup3); // false bool IsAdmin1_4 = currentUser1.IsInRole(adminGroup4); // false bool IsAdmin1_5 = currentUser1.IsInRole(adminGroup5); // false WindowsPrincipal currentUser2 = new WindowsPrincipal(WindowsIdentity.GetCurrent()); bool IsAdmin2_1 = currentUser2.IsInRole(adminGroup1); // false bool IsAdmin2_2 = currentUser2.IsInRole(adminGroup2); // false bool IsAdmin2_3 = currentUser2.IsInRole(adminGroup3); // false bool IsAdmin2_4 = currentUser1.IsInRole(adminGroup4); // false bool IsAdmin2_5 = currentUser2.IsInRole(adminGroup5); // false Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } 

Why all the above checks do not work?

+4
source share
1 answer

to try

 currentUser1.IsInRole(WindowsBuiltInRole.Administrator) 

See MSDN .

"In Windows Vista and later versions of Windows, user account control (UAC) defines user privileges. [..] Code that executes the IsInRole method does not display the Consent dialog box. Returns false if you are in the standard user role, even if you’re in the Embedded Admins group

+5
source

Source: https://habr.com/ru/post/1396818/


All Articles