Warum kehrt Windowssprincipal.issinrole immer falsch für die Gruppe „Administratoren“ zurück?
-
26-10-2019 - |
Frage
Mein lokales Benutzerkonto befindet sich in der Administratorengruppe, und ich wollte einfach herausfinden, wie ein Windows -Formularprojekt feststellen würde, ob ich in der Administratorengruppe bin. Also habe ich ein Windows Forms -Projekt gestartet und Folgendes ausprobiert:
[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());
}
Warum scheitern alle oben genannten Überprüfungen?
Lösung
Versuchen
currentUser1.IsInRole(WindowsBuiltInRole.Administrator)
Sehen Msdn.
"In Windows Vista und späteren Versionen des Windows -Betriebssystems bestimmt die Benutzerkontrolle (UAC) die Berechtigungen eines Benutzers. [..] Der Code, der die ISInROLE -Methode ausführt Sie sind in der Standardbenutzerrolle, auch wenn Sie sich in der integrierten Administratorengruppe befinden. "
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow