c# - 检查 Active Directory 角色花费的时间太长

标签 c# active-directory

我有这段代码来检查群组成员资格,但它似乎花费了太长时间来响应并减慢了我的应用程序,几乎需要 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/

相关文章:

windows - Win32 : How to validate credentials against Active Directory?

具有多个 Active Directory 服务器的 Grails Spring Security LDAP 插件

c# - { 得到;放; } 在变量声明中

c# - 在 UWP PasswordBox 中过滤字符的正确方法是什么?

c# - 结构的默认参数

c# - 在c#中获取Active Directory组的用户名

Java:LDAP 搜索返回 1 行

c# - 图像隐写术

c# - asp.net C# ajax 运行 'async' 批处理作业

arrays - 如何将 AD 计算机名称传递给数组?