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