简体   繁体   English

为什么WindowsPrincipal.IsInRole总是为“管理员”组返回false?

[英]Why is WindowsPrincipal.IsInRole always returning false for the “Administrators” group?

My local user account is in the Administrators group, and I wanted to simply figure out how a windows forms project would determine if I'm in the administrators group. 我的本地用户帐户位于Administrators组中,我想简单地弄清楚Windows窗体项目将如何确定我是否在管理员组中。 So, I started a windows forms project and tried the following: 所以,我启动了一个Windows窗体项目,并尝试了以下内容:

[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 are all the above checks failing? 为什么上述所有检查都失败了?

try 尝试

currentUser1.IsInRole(WindowsBuiltInRole.Administrator)

See MSDN . 请参阅MSDN

"In Windows Vista and later versions of the Windows operating system, User Account Control (UAC) determines the privileges of a user. [..] The code that executes the IsInRole method does not display the Consent dialog box. The code returns false if you are in the standard user role, even if you are in the Built-in Administrators group" “在Windows Vista和更高版本的Windows操作系统中,用户帐户控制(UAC)确定用户的权限。[..]执行IsInRole方法的代码不显示”同意“对话框。如果代码返回false,则代码返回false即使您在内置管理员组中,您也处于标准用户角色中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WindowsPrincipal.IsInRole() 没有返回预期的结果 - WindowsPrincipal.IsInRole() not returning the expected result WindowsPrincipal.IsInRole考虑组内的组吗? - WindowsPrincipal.IsInRole takes into account groups inside groups? WindowsPrincipal.IsInRole和通用与全局活动目录组 - WindowsPrincipal.IsInRole and universal vs. global active directory groups 无法使WindowsPrincipal.IsInRole(“ domain \\\\ groupname”)正常工作 - Can't get WindowsPrincipal.IsInRole(“domain\\groupname”) to work WindowsPrincipal.IsInRole在IIS 7 / Win Server 2K8中不起作用,但在IIS 6 / Win Server 2K3中起作用 - WindowsPrincipal.IsInRole not working in IIS 7/Win Server 2K8, but working on IIS 6/Win Server 2K3 User.IsInRole("Admin") 在 _Layout.cshtml 中总是返回 false - User.IsInRole("Admin") returning always false in _Layout.cshtml Principal.IsInRole(“ AD Group Name”)始终返回false,不会引发任何异常 - Principal.IsInRole(“AD Group Name”) always returns false, no exceptions thrown 为什么NewRow总是返回false? - Why IsNewRow always returning false? User.IsInRole()在中间件中始终为false - User.IsInRole() always false in middleware Azure AAD ClaimsPrincipal IsInRole始终返回false - Azure AAD ClaimsPrincipal IsInRole always returns false
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM