我有这段代码来检查群组成员资格,但它似乎花费了太长时间来响应并减慢了我的应用程序,几乎需要 7-12 秒才能响应,我只需要检查一个特定的群组成员资格,是有更快的方法吗?
public static bool isInRole(UserAccount userAccount, string groupName)
{
using (var ctx = new PrincipalContext(ContextType.Domain, userAccount.DomainName))
{
using (var grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, groupName))
{
bool isInRole = grp != null &&
grp
.GetMembers(true)
.Any(m => m.SamAccountName == userAccount.UserName);
return isInRole;
}
}
最佳答案
我手头没有您的特定 AD 来测试这个 - 但它可能值得一试:与其检查特定用户(可能有数千名成员)的组成员,不如你为什么不检查用户的组成员资格以查看用户是否拥有正确的组??
类似的东西:
public static bool isInRole(UserAccount userAccount, string groupName)
{
using (var ctx = new PrincipalContext(ContextType.Domain, userAccount.DomainName))
using (var user = UserPrincipal.FindByIdentity(ctx, userAccount.UserName))
{
bool isInRole = user != null &&
user.GetAuthorizationGroups()
.Any(g => g.Name == groupName);
return isInRole;
}
}
也许那样做会更快一些?
关于c# - 检查 Active Directory 角色花费的时间太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6808788/