2012-09-11 30 views
8

gibi özellikler tarafından nasıl listelenir? Ekran adı, Telefon ve Bölüm gibi filtreler kullanarak Active Directory'de arama yapmam gerekiyor. Görünen ad ve telefon kolay ama departmanda takılıyorum. Bu işler biraz:Active Directory'deki Kullanıcıların listesi, Bölüm

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

Ama bu işe yaramazsa: Ben sadece böyle bir şey eklemek umuyorum

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

. Bunu nasıl yapabileceğimi bilen var mı?

Düzeltme: Ben bir aptalım, arama terimini değiştirdim ve cevabı bulundu. Ekstra alanlara katılımcılar denir. Thanks Raymund Macaalay for your blog article on extending Principals.

Benim genişletilmiş 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); } 
    } 
} 

cevap

5

Zaten Department özelliğini eklemeniz UserPrincipal genişletilmiş ettiğinizden dolayı sizi aramak istediğinizde kullanıcı anapara versiyonunu uzattığını kullanmanız gerekir.

bu deneyin:

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); 
      } 
     } 
    } 
} 
İlgili konular