我正在尝试使用一种方法来接收用户名,如果该用户是本地管理员(不是在整个域上,只是在本地机器上),则返回 true,否则返回 false。我试图改变In .NET/C# test if process has administrator permissions to work 中发现的技术,但它没有。我曾尝试使用 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 方法更好的方法?