2011-02-04 62 views
8

下面的代碼讓我的用戶組中,但它返回 "CN=johnson\,Tom,OU=Users,OU=Main,DC=company,DC=com"獲取用戶名通過.NET

我只想返回第一個和最後一個名字。我怎樣才能做到這一點?

DirectoryEntry ou = new DirectoryEntry(); 
DirectorySearcher src = new DirectorySearcher(); 

src.Filter = ("(&(objectClass=group)(CN=Gname))"); 
SearchResult res = src.FindOne(); 
if (res != null) 
{ 
    DirectoryEntry deGroup = new DirectoryEntry(res.Path); 
    PropertyCollection pcoll = deGroup.Properties; 

    foreach (object obj in deGroup.Properties["member"]) 
    { 
      ListBox1.Items.Add(obj.ToString()); 
    } 
} 
+0

下面是不是一個真正的答案,只是一個警告:從調用[DirectorySearcher.FindAll]換你的DirectoryEntry,和的DirectorySearcher特別任何SearchResultCollections(例如(HTTP ://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.findall.aspx))在using語句中,或try/finally與Dispose調用。 SearchResultCollections不能被垃圾收集。在使用我在網上找到的例子後,我查找了幾天的內存泄漏,但沒有處理任何內容。仔細檢查MS文檔以查看搜索Active Directory涉及哪些類hav – 2011-03-02 05:06:22

回答

22

我更喜歡使用System.DirectoryServices.AccountManagement類:

PrincipalContext principalContext = new PrincipalContext(ContextType.Domain); 
GroupPrincipal group = GroupPrincipal.FindByIdentity(principalContext, "GName"); 

搜索通過group.Members屬性,直到你有你想要一個主要。然後提取像這樣的名字:

foreach (Principal principal in group.Members) 
{ 
    string name = principal.Name; 
} 
+4

您需要添加對** System.DirectoryServices.AccountManagement **項目的引用。 – bigtlb 2011-02-04 19:20:38

+1

很好的回答!謝謝 – Eric 2011-02-04 19:30:46

2

使用你的代碼中,給定名稱名字)和SN)的屬性應該工作。

如果使用System.DIrectoryServices.AccountManagement命名空間UserPrincipal(如@拉塞爾 - 麥克盧爾建議),你會發現給定名稱性也。

除非您必須遍歷受信任的林並需要全局編錄才能找到用戶,否則AccountManagement非常方便。

0

這是我在不使用AccountManagement類的情況下做的一個PowerShell腳本。它應該很容易把它翻譯成C#:

[void][System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices"); 

$groupName = "Grupo Domain"; 

$directoryEntry = New-Object System.DirectoryServices.DirectoryEntry; 
$directorySearcher = New-Object System.DirectoryServices.DirectorySearcher($directoryEntry, "(&(objectClass=group)(CN=$groupName))"); 
[void]$directorySearcher.PropertiesToLoad.Add("objectSid"); 
[void]$directorySearcher.PropertiesToLoad.Add("member"); 
$result = $directorySearcher.FindOne(); 

if ($result -eq $null) { return; } 

# Try get the group members through the "member" property. 
if ($result.Properties["member"].Count -gt 0) { 
    foreach ($member in $result.Properties["member"]) { 
     $memberSearcher = New-Object System.DirectoryServices.DirectorySearcher($directoryEntry, "(&(objectClass=*)(distinguishedName=$member))"); 
     [void]$memberSearcher.PropertiesToLoad.Add("msDS-PrincipalName"); 
     $memberResult = $memberSearcher.FindOne(); 
     if ($memberResult -eq $null) { continue; } 
     Write-Output $memberResult.Properties["msDS-PrincipalName"]; 
    } 
    return; 
} 
if ($result.Properties["objectSid"].Count -gt 0) { 
    # The group might be an AD primary group. Try get the members by the PrimaryGroupID. 
    $groupSid = New-Object System.Security.Principal.SecurityIdentifier($result.Properties["objectSid"][0], 0); 
    # Hacky way to get only the last RID. 
    $primaryGroupSid = $groupSid.Value.Replace($groupSid.AccountDomainSid.ToString(), [String]::Empty).TrimStart('-'); 
    $memberSearcher = New-Object System.DirectoryServices.DirectorySearcher($directoryEntry, "(&(objectClass=*)(primaryGroupId=$primaryGroupSid))"); 
    [void]$memberSearcher.PropertiesToLoad.Add("msDS-PrincipalName"); 
    $memberResult = $memberSearcher.FindAll(); 
    if ($memberResult -eq $null) { continue; } 
    foreach ($member in $memberResult) { 
     Write-Output $member.Properties["msDS-PrincipalName"]; 
    } 
}