如何在C#中的Active Directory中的UserPrincipal对象上设置管理器属性

时间:2012-07-02 18:02:44

标签: c# active-directory

我正在尝试在此处记录的Manager类型的对象上设置属性UserPrincipal

http://msdn.microsoft.com/en-us/library/windows/desktop/ms680857(v=vs.85).aspx

但不能简单地说

UserPrincipal.Manager = "some value" 

有人可以向我解释一下这是如何运作的吗?谢谢!

2 个答案:

答案 0 :(得分:9)

S.DS.AM命名空间中的基本UserPrincipal不具有该属性 - 但您可以扩展用户主体类并添加所需的其他属性。

在这里阅读更多相关信息:

Managing Directory Security Principals in the .NET Framework 3.5
在文章末尾有可扩展性的部分

以下是代码:

[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);
    }
}

现在,您可以找到并使用具有UserPrincipalEx属性的.Manager类供您使用:

UserPrincipalEx userEx = UserPrincipalEx.FindByIdentity(ctx, "YourUserName");

// the .Manager property contains the DN (distinguished name) for the manager of this user
var yourManager = userEx.Manager;

答案 1 :(得分:3)

我喜欢这个例子,但完全可以得到RatBoyStl的来源。有时你只想要一个价值而不是一个新的类。

如果给定用户有UserPrinciple个对象,则可以使用此代码轻松检索manager属性。我更进一步,使用经理值查找他们的UserPrinciple并显示电子邮件地址。

            //set the principal context to the users domain
        PrincipalContext pc = new PrincipalContext(ContextType.Domain, userDomain);

        //lookup the user id on the domain
        UserPrincipal up = UserPrincipal.FindByIdentity(pc, userId);
        if (up == null)
        {
            Console.WriteLine(string.Format("AD USER NOT FOUND {0}", userGc));
            return;

        }

        //grab the info we need from the domain
        Console.WriteLine(up.ToString());

        DirectoryEntry d = up.GetUnderlyingObject() as DirectoryEntry;
        string managerCN = d.Properties["manager"].Value.ToString(); 
        Console.WriteLine(managerCN);

        UserPrincipal manager = UserPrincipal.FindByIdentity(pc, managerCN);
        Console.WriteLine(manager.EmailAddress);