2012-07-02 11 views

risposta

9

Lo standard UserPrincipal nello spazio dei nomi S.DS.AM non presenta questo attributo, ma è possibile estendere la classe principale utente e aggiungere ulteriori attributi necessari.

Per saperne di più su di esso qui:

Managing Directory Security Principals in the .NET Framework 3.5
(c'è una sezione sul estensibilità verso la fine di questo articolo)

Ecco il codice:

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

Ora puoi trovare e lavorare con una classe UserPrincipalEx che ha il .Manager immobili per l'uso:

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

Suppongo di doverlo fare per ogni Attributo che non è una parte "standard" della classe UserPrincipal come Indirizzo Email o attributi personalizzati come WorkCellPhone? – RatBoyStl

+0

@ user574376: sì - ma ovviamente, puoi avere una classe 'UserPrincipalEx' che contiene tutti quegli attributi - questo è quello che ho, il codice sopra è solo una piccola sezione della mia vera classe. –

+0

Non c'è un modo più semplice per farlo? Come chiamare PropertiesToLoad quando cerco l'oggetto UserPrincipal iniziale? Sembra molto lavoro da fare. – RatBoyStl

3

amo l'esempio ma totalmente arrivare dove la RatBoyStl proviene. A volte vuoi solo un valore e non una nuova classe.

Se si dispone di un oggetto UserPrinciple per un determinato utente, è possibile recuperare facilmente la proprietà di gestione con questo codice. Ho fatto un passo avanti e ho usato il valore manageriale per trovare il loro UserPrinciple e visualizzare l'indirizzo email.

  //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

Ho avuto la sfortuna di ottenere il PrincipalEx Save per salvare effettivamente in Active Directory, ma il GetUnderlyingObject() il metodo semplifica usando il vecchio modo di impostare Proprietà [X] ... Grazie. – Dscoduc

Problemi correlati