2012-07-02 6 views

Odpowiedz

9

Podstawowy UserPrincipal w przestrzeni nazw S.DS.AM nie ma tego atrybutu - można jednak rozszerzyć główną klasę użytkownika i dodać dodatkowe atrybuty, których potrzebujesz.

Czytaj więcej na ten temat tutaj:

Managing Directory Security Principals in the .NET Framework 3.5
(istnieje rozdział na rozciągliwości pod koniec artykułu)

Oto kod:

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

    // Implement the overloaded search method FindByIdentity. 
    public static new UserPrincipalEx FindByIdentity(PrincipalContext context, string identityValue) 
    { 
     return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityValue); 
    } 

    // Implement the overloaded search method FindByIdentity. 
    public static new UserPrincipalEx FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue) 
    { 
     return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityType, identityValue); 
    } 
} 

teraz możesz znaleźć i pracować z klasą UserPrincipalEx, która ma .Manager nieruchomość do użycia:

UserPrincipalEx userEx = UserPrincipalEx.FindByIdentity(ctx, "YourUserName"); 

// the .Manager property contains the DN (distinguished name) for the manager of this user 
var yourManager = userEx.Manager; 
+0

zakładam muszę to zrobić dla każdego atrybutu, który nie jest „standard” część klasy UserPrincipal takie jak adres e-mail lub niestandardowych atrybutów jak WorkCellPhone? – RatBoyStl

+0

@ user574376: tak - ale oczywiście możesz mieć jedną klasę 'UserPrincipalEx', która zawiera wszystkie te atrybuty - to właśnie mam, powyższy kod to tylko mała sekcja z mojej prawdziwej klasy. –

+0

Nie ma prostszego sposobu na zrobienie tego? Podobnie jak przez wywołanie właściwości PropertyToLoad podczas wyszukiwania początkowego obiektu UserPrincipal? Wygląda na to, że trzeba dużo pracy. – RatBoyStl

3

Kocham przykład, ale całkowicie się gdzie RatBoyStl pochodzi. Czasami potrzebujesz tylko wartości, a nie nowej klasy.

Jeśli masz obiekt UserPrinciple dla danego użytkownika, możesz łatwo pobrać właściwość menedżera z tym kodem. Zrobiłem krok dalej i użyłem wartości menedżera, aby znaleźć ich UserPrinciple i wyświetlić adres e-mail.

  //set the principal context to the users domain 
     PrincipalContext pc = new PrincipalContext(ContextType.Domain, userDomain); 

     //lookup the user id on the domain 
     UserPrincipal up = UserPrincipal.FindByIdentity(pc, userId); 
     if (up == null) 
     { 
      Console.WriteLine(string.Format("AD USER NOT FOUND {0}", userGc)); 
      return; 

     } 

     //grab the info we need from the domain 
     Console.WriteLine(up.ToString()); 

     DirectoryEntry d = up.GetUnderlyingObject() as DirectoryEntry; 
     string managerCN = d.Properties["manager"].Value.ToString(); 
     Console.WriteLine(managerCN); 

     UserPrincipal manager = UserPrincipal.FindByIdentity(pc, managerCN); 
     Console.WriteLine(manager.EmailAddress); 
+0

Miałem straszne szczęście, że udało mi się zapisać PrincipalEx Save w rzeczywistości w Active Directory, ale GetUnderlyingObject() metoda upraszcza korzystanie ze starego sposobu ustawiania właściwości [X] ... Dziękuję. – Dscoduc

Powiązane problemy