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;
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
@ 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. –
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