Searching Active Directory for a user with a certain property using DirectoryServices.AccountManagement

StackOverflow https://stackoverflow.com/questions/8656308

문제

I'm new to accessing Active Directory and I was advised to use the System.DirectoryServices.AccountManagement namespace but I don't know how to search in it for a user with a certain initials.

Any help ?

도움이 되었습니까?

해결책

Here is a full sample using PrincipalSearcher, even with your own attributes if you want (the code is as is).

/* 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());

With this definition for 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); }
  }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top