I have a code which is getting users from a specific group.
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainName);
GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, groupName);
if (grp != null)
{
foreach (Principal p in grp.GetMembers(true))
{
Console.WriteLine(p.Name);
}
}
The problem is that i am not able to get users Mobile phone, Home phone, Department, Country. Has someone any ideas how it can be done using this method ?
The ExtensionGet method in @HuroSwords answer cannot be used directly as it is a protected method.
As is mentioned elsewhere you'll need to create your own child class to use it. I've included a sample of what I've done in the past to get extra user attributes below.
[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("User")]
public class UserPrincipalExtended : UserPrincipal
{
public UserPrincipalExtended(PrincipalContext context) : base(context)
{
}
// Implement the overloaded search method FindByIdentity to return my extended type
public static new UserPrincipalExtended FindByIdentity(PrincipalContext context, string identityValue)
{
return (UserPrincipalExtended)FindByIdentityWithType(context, typeof(UserPrincipalExtended), identityValue);
}
// Implement the overloaded search method FindByIdentity to return my extended type
public static new UserPrincipalExtended FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
{
return (UserPrincipalExtended)FindByIdentityWithType(context, typeof(UserPrincipalExtended), identityType, identityValue);
}
[DirectoryProperty("physicalDeliveryOfficeName")]
public string Department
{
get
{
if (ExtensionGet("physicalDeliveryOfficeName").Length != 1)
return null;
return (string)ExtensionGet("physicalDeliveryOfficeName")[0];
}
}
}
Then use the child class like you would a normal UserPrincipal object.
var domain = new PrincipalContext(ContextType.Domain);
var userPrincipal = UserPrincipalExtended.FindByIdentity(domain, HttpContext.Current.User.Identity.Name);
Console.WriteLine(userPrincipal.Location);
In your case you may need to re-fetch the principal.