加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 检查Active Directory角色耗时太长

发布时间:2020-12-15 21:21:11 所属栏目:百科 来源:网络整理
导读:我有这个代码检查组成员身份,但似乎需要太长时间来响应和减慢我的应用程序,它需要几乎7-12秒的响应,我只需要检查一个特定的组成员,是否有更快这样做的方法? public static bool isInRole(UserAccount userAccount,string groupName) { using (var ctx = new
我有这个代码检查组成员身份,但似乎需要太长时间来响应和减慢我的应用程序,它需要几乎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;
   }
}

也许这样的事情会更快一点?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读