2014-11-20 4 views
1

Principal класс только иметь атрибуты несколько AD:Как использовать AD Атрибуты не представлены в Основном классе

enter image description here

Проблема в том, мне нужно прочитать свойство, которое не в Principal класса. ..

Вот как я запрашиваю объект AD:

// create your domain context 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain,ConfigurationManager.AppSettings["ADDomain"].ToString(), ConfigurationManager.AppSettings["ADUser"].ToString(), ConfigurationManager.AppSettings["ADPassword"].ToString()); 

// define a "query-by-example" principal - here, we search for all users 
UserPrincipalEXT qbeUser = new UserPrincipalEXT(ctx); 

// create your principal searcher passing in the QBE principal  
PrincipalSearcher srch = new PrincipalSearcher(qbeUser); 

// find all matches 
foreach (var found in srch.FindAll()) //FOUND represent the AD object 
{ 
    ... 
} 

есть ли способ продлить Principal класс для получения дополнительных свойств AD?

ответ

2

Вы можете использовать GetUnderlyingObject(), чтобы получить доступ к дополнительным свойствам:

if (found.GetUnderlyingObjectType() == typeof(DirectoryEntry)) 
{ 
    DirectoryEntry de = (DirectoryEntry)principal.GetUnderlyingObject(); 
    // Use de.Properties to access additional information 
} 
3

Если вы на .NET 3.5 и выше, и с помощью System.DirectoryServices.AccountManagement (S.DS.AM) пространства имен, вы можете легко расширить существующий UserPrincipal класс, чтобы получить на более продвинутых свойств, как Manager и т.д.

Читайте об этом здесь:

В принципе, вы просто определить производный класс, основанный на UserPrincipal, а затем определить свои дополнительные свойства, которые вы хотите:

[DirectoryRdnPrefix("CN")] 
[DirectoryObjectClass("Person")] 
public class UserPrincipalEx : UserPrincipal 
{ 
    // Inplement the constructor using the base class constructor. 
    public UserPrincipalEx(PrincipalContext context) : base(context) 
    { } 

    // Implement the constructor with initialization parameters.  
    public UserPrincipalEx(PrincipalContext context, 
         string samAccountName, 
         string password, 
         bool enabled) : base(context, samAccountName, password, enabled) 
    {} 

    // Create the "Department" property.  
    [DirectoryProperty("department")] 
    public string Department 
    { 
     get 
     { 
      if (ExtensionGet("department").Length != 1) 
       return string.Empty; 

      return (string)ExtensionGet("department")[0]; 
     } 
     set { ExtensionSet("department", value); } 
    } 

    // Create the "Manager" property.  
    [DirectoryProperty("manager")] 
    public string Manager 
    { 
     get 
     { 
      if (ExtensionGet("manager").Length != 1) 
       return string.Empty; 

      return (string)ExtensionGet("manager")[0]; 
     } 
     set { ExtensionSet("manager", value); } 
    } 
} 

Теперь вы можете использовать «расширенную» версию UserPrincipalEx в вашем коде:

using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain)) 
{ 
    // Search the directory for the new object. 
    UserPrincipalEx inetPerson = UserPrincipalEx.FindByIdentity(ctx, IdentityType.SamAccountName, "someuser"); 

    // you can easily access the Manager or Department now 
    string department = inetPerson.Department; 
    string manager = inetPerson.Manager; 
}   
+0

Действительно хороший ответ. Спасибо!! – Shiva

Смежные вопросы