【问题标题】:Searching Active Directory for a user with a certain property using DirectoryServices.AccountManagement使用 DirectoryServices.AccountManagement 在 Active Directory 中搜索具有特定属性的用户
【发布时间】:2011-12-28 13:10:26
【问题描述】:

我是访问 Active Directory 的新手,有人建议我使用 System.DirectoryServices.AccountManagement 命名空间,但我不知道如何在其中搜索具有特定首字母缩写的用户。

有什么帮助吗?

【问题讨论】:

  • @marc_s 任何帮助

标签: .net active-directory account-management


【解决方案1】:

这是一个使用PrincipalSearcher 的完整示例,如果您愿意,也可以使用您自己的属性(代码原样)。

/* Looking for users
 */
PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "WM2008R2ENT:389", "dc=dom,dc=fr", "jpb", "root.123");

/* Create a user principal to look for
 */
slxUser aSlxUser = new slxUser(domainContext);
aSlxUser.streetAddress = "The Adress"

/* FindAll
 */
PrincipalSearchResult<Principal> results = new PrincipalSearcher(aSlxUser).FindAll();
  Console.WriteLine(results.Count());

有了 slxUser 的这个定义:

[DirectoryObjectClass("user")]
[DirectoryRdnPrefix("CN")]
class slxUser : UserPrincipal
{
  public slxUser(PrincipalContext context)
    : base(context) { }

  public slxUser(PrincipalContext context, string samAccountName, string password,  bool enabled ) : base(context, samAccountName, password, enabled)
  {
  }

  [DirectoryProperty("streetAddress")]
  public string streetAddress
  {
    get
    {
      object[] result = this.ExtensionGet("streetAddress");
      if (result != null)
      {
        return (string)result[0];
      }
      else
      {
        return null;
      }
    }
    set { this.ExtensionSet("streetAddress", value); }
  }
}

【讨论】:

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