2013-03-25 7 views
6

Avviato utilizzando lo spazio dei nomi System.DirectoryServices.AccountManagement, per eseguire la ricerca su un utente nella directory attiva (AD). Ho anche bisogno del gestore dell'utente, ma mi sembra di aver colpito un bernoccolo usando questo spazio dei nomi. codice attuale per ottenere una persona:C# - Ricerca di un gestore utenti nella directory attiva

class Person { 
    // Fields 
    public string GivenName = null; 
    public string Surname = null; 
    public string DistinguishedName = null; 
    public string Email = null; 
    public string MangerDistinguishedName = null; // Unable to set this 

    // Constructor 
    public Person(string userName) { 
     UserPrincipal user = null; 

     try { 
      user = GetUser(userName); 

      if (user != null) { 
       this.GivenName = user.GivenName; 
       this.Surname = user.Surname; 
       this.DistinguishedName = user.DistinguishedName; 
       this.Email = user.EmailAddress; 
       this.MangerDistinguishedName = user.<NO SUCH PROPERTY TO FIND A MANAGER'S DISTINGUISHED NAME> 
      } 
      else { 
       throw new MissingPersonException("Person not found"); 
      } 
     } 
     catch (MissingPersonException ex) { 
      MessageBox.Show(
       ex.Message 
       , ex.reason 
       , MessageBoxButtons.OK 
       , MessageBoxIcon.Error 
      ); 
     } 
     catch (Exception ex) { 
      MessageBox.Show(
       ex.Message 
       , "Error: Possible connection failure, or permissions failure to search for the username provided." 
       , MessageBoxButtons.OK 
       , MessageBoxIcon.Error 
      ); 
     } 
     finally { 
      user.Dispose(); 
     } 
    } 

Eseguire di ricerca per la persona

private UserPrincipal GetUser(string userName) { 
     PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 
     UserPrincipal user = UserPrincipal.FindByIdentity(ctx, userName); 

     return user; 
    } 

Che è un altro modo per accedere direttamente il nome distinto del gestore di un particolare utente?

  • possibile risposta parziale here in VB, ma non vedo nulla di riferimento per i manager.
  • Un'altra possibile parziale here, ancora una volta, nulla sui gestori.

risposta

7

Se siete su .NET 3.5 e su e utilizzando la (S.DS.AM) namespace System.DirectoryServices.AccountManagement, si può facilmente estendere la UserPrincipal classe esistente per ottenere le proprietà più avanzate, come Manager ecc

Leggi tutto su di esso qui:

In pratica, basta definire una classe derivata basata su UserPrincipal, e poi si definiscono le proprietà aggiuntive che si desidera:

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

    // 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, è possibile utilizzare la versione "estesa" del UserPrincipalEx nel codice:

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

Hai provato questo? Non funziona, per me. UserPrincipalEx.FindByIdentity non restituisce un oggetto UserPrincipalEx e il casting in UserPrincipalEx causa una InvalidCastException. – Naikrovek

+1

@Naikrovek: scusa, errore mio, ho ritagliato un po 'troppo codice dal mio esempio (molto più lungo). Avevo perso i due metodi 'FindByIdentity' e' FindByIdentityWithType' sovraccaricati - li ho aggiunti al mio snippet di codice - e sì, con questo codice, l'ho appena controllato su Active Directory di Windows Server 2008 R2 e funziona perfettamente per me. –

+0

Funziona alla grande, grazie. – Naikrovek

Problemi correlati