简体   繁体   English

在Framework 4.5中获取活动目录用户属性

[英]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. 尝试在foreach循环中声明UserPrincipal而不是Principal ,以允许您使用专门为Active Directory用户定义的属性。

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. ExtensionGet方法使您不仅可以从标准Active Directory字段中检索数据,还可以从目录中包括的其他自定义数据中检索数据。

The ExtensionGet method in @HuroSwords answer cannot be used directly as it is a protected method. @HuroSwords答案中的ExtensionGet方法不能直接使用,因为它是受保护的方法。

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. 然后像使用普通UserPrincipal对象一样使用子类。

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. 在您的情况下,您可能需要重新获取主体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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