2011-04-11 65 views
1

我想知道是否有快速的方式从Active Directory查询信息。从Active Directory中查询用户的组成员资格

具体来说,我试图查询当前用户的“成员”组,以给定字符串开头,例如说“abc-”。

如果有人能帮助我,我会非常感激。

+0

欢迎来到SO,Matt!为了清晰起见,我编辑了您的问题。如果您不喜欢我所做的更改或希望进一步修改您的问题,请使用“编辑”链接进行所需的更改。 – Greg 2011-04-11 15:20:09

回答

2

你可以以不同的方式,Managing Directory Security Principals in the .NET Framework 3.5 帮助您这样说:

static void Main(string[] args) 
{ 
    /* Retreiving a principal context 
    */ 
    PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "WM2008R2ENT", "dc=dom,dc=fr", "TheUser", "ThePassword"); 

    /* Discribe the group You are looking for as a principal 
    */ 
    GroupPrincipal gpPrincipal = new GroupPrincipal(domainContext); 
    gpPrincipal.Name = "abc-*"; 

    /* Bind a searcher 
    */ 
    PrincipalSearcher searcher = new PrincipalSearcher(); 
    searcher.QueryFilter = gpPrincipal; 

    PrincipalSearchResult<Principal> hRes = searcher.FindAll(); 

    /* Read The result 
    */ 
    foreach (GroupPrincipal grp in hRes) 
    { 
    Console.WriteLine(grp.Name); 
    // You are looking for "grp.Members" 
    } 

    Console.ReadLine(); 
} 

我希望它能帮助。

相关问题