gpt4 book ai didi

c# - 使用 UserPrincipal 检查是否是本地管理员

转载 作者:行者123 更新时间:2023-11-30 15:38:31 24 4
gpt4 key购买 nike

我正在尝试使用一种方法来接收用户名,如果该用户是本地管理员(不是在整个域中,只是在本地计算机上),则返回 true,否则返回 false。我试图改变在 In .NET/C# test if process has administrative privileges 找到的技术工作,但它没有。我尝试过使用 NetUserGetInfo 方式,但无法正常工作。现在我正在尝试使用 UserPrincipal。下面的代码是我所有的……主要是测试基础知识是否有效,它们是否有效。

PrincipalContext ctx = new PrincipalContext(ContextType.Machine);
UserPrincipal usr = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, userId);

if(usr == null)
{
Console.WriteLine("usr is null");
}
else
{
Console.WriteLine(usr.Enabled);
Console.WriteLine(usr.IsAccountLockedOut());

foreach (Principal p in usr.GetAuthorizationGroups())
{
Console.WriteLine(p.ToString());
}
}

看起来我应该可以使用 isMemberOf 方法,但是如何为本地管理员创建一个组?还是有比 isMemberOf 方法更好的方法?

最佳答案

实际上我可以检查从 GetAuthorizationGroups()) 返回的其中一个主体是否等于“管理员”。

foreach (Principal p in usr.GetAuthorizationGroups())
{
if (p.ToString() == "Administrators")
{
result = true;
}
}

关于c# - 使用 UserPrincipal 检查是否是本地管理员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11296569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com