【问题标题】:How to get a UserPrincipal object for the manager from the employee's UserPrincipal object如何从员工的 UserPrincipal 对象中获取经理的 UserPrincipal 对象
【发布时间】:2016-05-05 13:11:13
【问题描述】:

我有一个 Active Directory 搜索器,可以使用 UserPrincipal 对象获取用户的详细信息。我正在使用System.DirectoryServicesSystem.DirectoryServices.AccountManagement,它通过上下文访问AD。

但是,我还需要将用户的经理作为单独的UserPrincipal 对象。最直接的方法是什么?

我尝试了以下方法,但不幸的是,演员阵容不起作用:

DirectoryEntry directoryEntry = (DirectoryEntry)userPrincipal.GetUnderlyingObject();
UserPrincipal manager = (UserPrincipal)directoryEntry.Properties["manager"][0];

我希望在每个 UserPrincipal 对象中都有一个名为 UsersManagerUserPrincipal 属性,但我找不到这个,所以我猜没有这样的东西。

谢谢!

【问题讨论】:

    标签: c# active-directory directoryservices


    【解决方案1】:

    manager 属性将为您提供经理帐户的专有名称,它只是一个字符串。因此,您必须使用该 DN 查找经理的帐户。

    这可能有效(假设您已经有一个 context 对象):

    UserPrincipal manager = UserPrincipal.FindByIdentity(context, IdentityType.DistinguishedName, directoryEntry.Properties["manager"][0].ToString());
    

    【讨论】:

    • 那个没用,但这个没用: UserPrincipal manager = UserPrincipal.FindByIdentity(context, directoryEntry.Properties["manager"][0].ToString());所以我接受你的回答,谢谢:)
    • 未经测试的代码 :) 我很高兴它有所帮助。我会更新我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    相关资源
    最近更新 更多