2012-09-11 9 views
8

Ich muss eine Suche in Active Directory mithilfe von Filtern wie Anzeigename, Telefon und Abteilung bereitstellen. Anzeigename und Telefon sind einfach, aber ich bin auf Abteilung fest. Dies ist das Bit, das funktioniert:So erhalten Sie eine Liste von Benutzern aus Active Directory nach Attributen wie Abteilung

using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) 
{ 
    UserPrincipal userPrincipal = new UserPrincipal(context); 

    if (txtDisplayName.Text != "") 
     userPrincipal.DisplayName = "*" + txtDisplayName.Text + "*"; 

    using (PrincipalSearcher searcher = new PrincipalSearcher(userPrincipal)) 
    { 
     foreach (Principal result in searcher.FindAll()) 
     { 
      DirectoryEntry directoryEntry = result.GetUnderlyingObject() as DirectoryEntry; 
      DataRow drName = dtProfile.NewRow(); 
      drName["displayName"] = directoryEntry.Properties["displayName"].Value; 
      drName["department"] = directoryEntry.Properties["department"].Value; 
      dtProfile.Rows.Add(drName); 
     } 
    } 
} 

Ich hatte gehofft, dass ich nur so etwas wie könnte hinzufügen:

DirectoryEntry userDirectoryEntry = userPrincipal.GetUnderlyingObject() as DirectoryEntry; 
if (ddlDepartment.SelectedValue != "") 
    userDirectoryEntry.Properties["title"].Value = ddlDepartment.SelectedValue; 

Aber das funktioniert nicht. Weiß jemand wie ich das machen kann?

Edit: Ich bin ein Idiot, änderte den Suchbegriff und fand die Antwort. Die zusätzlichen Felder werden als Attribute bezeichnet. Thanks Raymund Macaalay for your blog article on extending Principals.

Meine erweiterten Userprincipal:

[DirectoryObjectClass("user")] 
[DirectoryRdnPrefix("CN")] 

public class UserPrincipalExtended : UserPrincipal 
{  
    public UserPrincipalExtended(PrincipalContext context) : base(context) 
    { 
    } 
    [DirectoryProperty("department")] 
    public string department 
    { 
     get 
     { 
      if (ExtensionGet("department").Length != 1) 
       return null; 
      return (string)ExtensionGet("department")[0]; 
     } 
     set { this.ExtensionSet("department", value); } 
    } 
} 

Antwort

5

Da Sie bereits die UserPrincipal erweitert sind das Department Attribut, Sie verwenden müssen, dass Version des Benutzerprinzipal verlängert, wenn Sie suchen möchten.

Versuchen Sie folgendes:

using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) 
{ 
    UserPrincipalExtended userPrincipal = new UserPrincipalExtended(context); 

    if (txtDisplayName.Text != "") 
    { 
     userPrincipal.DisplayName = "*" + txtDisplayName.Text + "*"; 
    } 

    if (!string.IsNullOrEmpty(txtDepartment.Text.Trim()) 
    { 
     userPrincipal.department = txtDepartment.Text.Trim(); 
    } 

    using (PrincipalSearcher searcher = new PrincipalSearcher(userPrincipal)) 
    { 
     foreach (Principal result in searcher.FindAll()) 
     { 
      UserPrincipalExtended upe = result as UserPrincipalExtended; 

      if (upe != null) 
      { 
       DataRow drName = dtProfile.NewRow(); 
       drName["displayName"] = upe.DisplayName; 
       drName["department"] = upe.department; 
       dtProfile.Rows.Add(drName); 
      } 
     } 
    } 
} 
Verwandte Themen