简体   繁体   English

如何从员工的UserPrincipal对象获取管理器的UserPrincipal对象

[英]How to get a UserPrincipal object for the manager from the employee's UserPrincipal object

I have an Active Directory searcher to get users' details using UserPrincipal objects. 我有一个Active Directory搜索器,可以使用UserPrincipal对象获取用户的详细信息。 I'm using System.DirectoryServices and System.DirectoryServices.AccountManagement and it's accessing AD through a context. 我正在使用System.DirectoryServicesSystem.DirectoryServices.AccountManagement ,它正在通过上下文访问AD。

However, I also need to get the user's manager as a separate UserPrincipal object. 但是,我还需要将用户的管理器作为单独的UserPrincipal对象。 What would be the most direct way to go about this? 最直接的方法是什么?

I have tried the following but unfortunately the cast won't work: 我尝试了以下但不幸的是演员阵容不起作用:

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

I hoped there would be a UserPrincipal property called UsersManager inside every UserPrincipal object but I'm unable to find this so I'm guessing there's no such thing. 我希望在每个UserPrincipal对象中都会有一个名为UsersManagerUserPrincipal属性,但是我无法找到它,所以我猜测没有这样的东西。

Thanks! 谢谢!

The manager attribute will give you the distinguished name of the manager's account, which is just a string. manager属性将为您提供经理帐户的可分辨名称,该帐户只是一个字符串。 So you'll have to look up the manager's account using that DN. 因此,您必须使用该DN查找经理的帐户。

This might work (assuming you already have a context object): 这可能有用(假设你已经有了一个context对象):

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

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

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