2013-01-09 50 views
0

我有ASP.NET和Active Directory的问题。如何使用SubGroups在Active Directory组中找到用户?

我想知道用户是否在Active Directory的一个团队中,如果他在这个组中,他可以看到更多。为此,我使用filterstring编写函数。问题在于,在我们公司我们切换组,结构不是静态的。为此,我搜索小组第一,比我搜索用户在集团与参数成员的...

这里是我们的广告结构:

enter image description here

这里是我的代码对于saerch组:

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; 
      } 
     } 

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; 
     } 

我对找到用户的方法

如何在组的子组中搜索用户并使其动态? :(

回答

1

没有尝试,但这种增加对过滤器的帮助? 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))"; 
+0

我试过但它不起作用;( – Tarasov

1

如果你在.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组中!

相关问题