繁体   English   中英

如何在具有子组的Active Directory组中找到用户?

[英]How I can find a user in Active Directory Group with SubGroups?

我有ASP.NET和Active Directory的问题。

我想知道用户是否在Active Directory的Groupe中,如果他在这个组中,他可以看到更多。 为此我用一个filterstring编写一个Function。 问题是,在我们公司,我们切换组,结构不是静态的。 为此,我首先搜索组,然后搜索组中的用户,参数member-of ...

这是我们AD的结构:

在此输入图像描述

这是我的搜索组的代码:

public string GetGroup(string groupname)
        {
            string path = "<OurDomain>";

            DirectoryEntry rootEntry = new DirectoryEntry(path);

            DirectorySearcher srch = new DirectorySearcher(rootEntry);
            srch.SearchScope = SearchScope.Subtree;

            srch.Filter = "(&(objectCategory=Group)(name=" + groupname + "))";

            SearchResult resFilter = srch.FindOne();

            string filterpath = resFilter.Path;

            return filterpath; 
        }

我找到用户的方法:

public bool IsUserInGroup(string username,string groupepath) 
        {
            string path = "<OurDomain>"; 

            DirectoryEntry rootEntry = new DirectoryEntry(path);

            DirectorySearcher srch = new DirectorySearcher(rootEntry);
            srch.SearchScope = SearchScope.Subtree;

            srch.Filter = "(&(objectClass=user)(sAMAccountName=*" + username + "*)(memberof=CN=GastzugangUser,OU=SubFolderB,OU=FolderB,DC=company,DC=com))";


            SearchResultCollection res = srch.FindAll();

            if (res == null || res.Count <= 0)
            {
                return false;
            }
            else
            {
                return true; 
            }
        }

如何在组的子组中搜索用户和该动态? :(

没有尝试,但将此添加到过滤器帮助? http://ldapwiki.willeke.com/wiki/1.2.840.113556.1.4.1941

例如

(&(objectClass=user)(sAMAccountName=*" + username + "*)(memberof:1.2.840.113556.1.4.1941:=CN=GastzugangUser,OU=SubFolderB,OU=FolderB,DC=company,DC=com))";

如果您使用的是.NET 3.5及更高版本,则应该查看System.DirectoryServices.AccountManagement (S.DS.AM)命名空间。 在这里阅读所有相关内容:

基本上,您可以定义域上下文并轻松查找AD中的用户和/或组:

// set up domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
  // find a user
  UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

  if(user != null)
  {
      // GetAuthorizationGroups returns a list of GroupPrincipals and work recursively
      var groupsForUser = user.GetAuthorizationGroups();

      // then check to see if that group you want it part of this list
  }
}

新的S.DS.AM使得在AD中与用户和群组玩游戏变得非常容易!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM