简体   繁体   中英

Get active directory user attributes in Framework 4.5

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 ?

Try to declare UserPrincipal instead Principal in your foreach loop to allow you use the properties specifically defined for Active Directory Users.

Or you can try to include this code on your loop.

Console.WriteLine(p.ExtensionGet("mobile")); // Mobile Phone
Console.WriteLine(p.ExtensionGet("homePhone"));  // Home Phone
Console.WriteLine(p.ExtensionGet("department"));  // Department
Console.WriteLine(p.ExtensionGet("co")); // Country

The method ExtensionGet allows you, not only retrieve data from standard Active Directory fields, rather than other customized data included on your catalog.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM