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;
fonte
2012-07-02 18:07:52
Suppongo di doverlo fare per ogni Attributo che non è una parte "standard" della classe UserPrincipal come Indirizzo Email o attributi personalizzati come WorkCellPhone? – RatBoyStl
@ 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. –
Non c'è un modo più semplice per farlo? Come chiamare PropertiesToLoad quando cerco l'oggetto UserPrincipal iniziale? Sembra molto lavoro da fare. – RatBoyStl