【发布时间】:2016-04-04 09:57:18
【问题描述】:
我有一些代码可以检查域用户是否是机器管理员组的成员:
public static bool ActiveDirectoryGroupMembershipOk(string userid, string groupName)
{
using (PrincipalContext ctx = new PrincipalContext(ContextType.Machine, "my_pc_name"))
{
using (GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, "administrators"))
{
if (grp != null)
{
foreach (Principal p in grp.GetMembers(false))
{
if (p is UserPrincipal && p.SamAccountName.Equals(userid, StringComparison.InvariantCultureIgnoreCase))
{
return true;
}
}
}
}
}
return false;
}
它可以工作,但下面的代码行需要几秒钟才能完成:
using (GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, "administrators"))
有没有更快的方法来查找会员资格?
不知道重要不重要,但是userid是域用户,windows组在本地PC上。
【问题讨论】:
-
根据我的经验,AD 总是很慢,所以我倾向于缓存结果。在你的情况下,我会使用一个类全局变量 grp 并调用 FindByidentity 一次
-
我为每个用户这样做。所以这是我的第一个请求,这很慢。我刚刚阅读了一篇关于查看用户成员而不是组成员的不同帖子 - 这可能会更快。 Link 但我无法让该代码工作。
标签: c# active-directory ldap-query