8

Estensione di UserPrincipal per sfruttare le sue proprietà incorporate ... in esecuzione in un problema quando sovraccarichiamo il metodo FindByIdentity().Estensione UserPrincipal; FindByIdentity() non riesce

dall'esempio di Microsoft al http://msdn.microsoft.com/en-us/library/bb384372%28VS.90%29.aspx (parti escluse per brevità):

[DirectoryRdnPrefix("CN")] 
[DirectoryObjectClass("inetOrgPerson")] 
public class InetOrgPerson : UserPrincipal { 

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

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

Se prendo il codice esatto dall'esempio MSDN e incollarlo nella mia app, non funziona. La chiamata a InetOrgPerson.FindByIdentity() restituisce NULL, in quanto tale:

if (null == InetOrgPerson.FindByIdentity(principalContext, UserName)) { 
    throw new Exception("bah"); 
} 

Infatti, dall'interno InetOrgPerson.FindByIdentity(), la chiamata a FindByIdentityWithType() restituisce NULL, in quanto tale:

if (null == FindByIdentityWithType(context, typeof(InetOrgPerson), identityType, identityValue) { 
    throw new Exception("bah"); 
} 

Tuttavia, la chiamata:

FindByIdentityWithType(context, typeof(UserPrincipal), identityType, identityValue) 

mi fornisce l'oggetto utente desiderato. Tranne che non posso usarlo, perché non può essere lanciato sull'oggetto InetOrgPerson che devo restituire.

Cosa dà? Mi aspetto che il codice di esempio di Microsoft funzioni, ma non lo fa, quindi naturalmente anche il codice che sto cercando di scrivere in base all'esempio non funziona. Qualcuno ha fatto questo lavoro?

Grazie in anticipo! James

risposta

12

Assicurarsi che l'utente che si sta cercando appartenga effettivamente alla classe inetOrgPerson.

+2

Sì, quello era il problema. Non mi ero reso conto che l'attributo 'DirectoryObjectClass' che ho impostato legava la classe a una classe in AD. Così ora capisco che quando faccio una ricerca attraverso 'FindByIdentity 'di questa classe, sto limitando la mia ricerca agli oggetti in AD della classe' inetOrgPerson ', di cui non ce ne sono nel nostro AD. Nel mio caso, voglio impostare 'DirectoryObjectClass' su 'utente'. Questo è davvero bello. Grazie! –

+0

Incredibile, ha risolto un problema anche per me – nokturnal

Problemi correlati