【问题标题】:get user names in an Active Directory Group via .net通过 .net 获取 Active Directory 组中的用户名
【发布时间】:2011-06-21 13:35:28
【问题描述】:

下面的代码让我获得了组中的用户,但它被返回了 "CN=johnson\,Tom,OU=Users,OU=Main,DC=company,DC=com"

我只想返回名字和姓氏。我怎样才能做到这一点?

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

【问题讨论】:

  • 以下并不是真正的答案,只是一个警告:将您的 DirectoryEntry、DirectorySearcher 尤其是任何 SearchResultCollections(例如,通过调用 DirectorySearcher.FindAll)包装在 using 语句中,或者使用 try/finally处理呼叫。 SearchResultCollections 不能被垃圾收集。在使用我在网上找到的一个没有 Dispose 任何东西的示例后,我花了几天时间寻找内存泄漏。彻底检查 MS 文档,了解搜索 Active Directory 所涉及的类

标签: c# .net visual-studio-2010 active-directory


【解决方案1】:

我更喜欢使用 System.DirectoryServices.AccountManagement 中的类:

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

搜索 group.Members 属性,直到找到所需的 Principal。然后像这样提取名称:

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

【讨论】:

  • 您需要在您的项目中添加对 System.DirectoryServices.AccountManagement 的引用。
【解决方案2】:

使用您的代码,givenName (first name) 和 sn (last name) 属性应该可以工作.

如果您使用 System.DIrectoryServices.AccountManagement 命名空间 UserPrincipal(如 @russell-mcclure 建议的那样),您还会发现 GivenNameSurname 属性。

AccountManagement 非常方便,除非您必须遍历受信任的林并需要全局目录才能找到用户。

【讨论】:

  • 我打赌他还需要设置 PropertiesToLoad msdn.microsoft.com/en-us/library/…
  • 在我在 .Net 4.0 下访问 Windows Server 2008 DC 的代码中,我得到了 24 个默认属性,而无需修改 DirectorySearcher 中的 PropertiesToLoad,包括 givenName 和 sn...
  • 我似乎无法使用 accountmanagement,因为它在我的 directoryservices 命名空间中不存在。
  • AccountManagement 在 3.5 和 4.0 msdn.microsoft.com/en-us/library/…
  • 您是否添加了对项目的引用?
【解决方案3】:

这是我在不使用 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"];
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多