通过employeeID获取UserPrincipal [英] Get UserPrincipal by employeeID

查看:90
本文介绍了通过employeeID获取UserPrincipal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了System.DirectoryServices.AccountManagement,用于身份验证进入我的Web应用程序,通过给定用户名的身份查找用户(UserPrincipal).但是,在几种情况下,我只需要给我一个employeeID来获取广告帐户.在AccountManagement中给定employeeID的情况下,有没有一种获取UserPrincipal(甚至只是sAMAccountName)的好方法?

I have implemented System.DirectoryServices.AccountManagement for authentication into my webapps finding users (UserPrincipal) byidentity given username. However, I have several cases where I need to get AD accounts given only an employeeID. Is there a good way to get a UserPrincipal (or even just the sAMAccountName) given an employeeID in AccountManagement?

我目前正在通过用户名来吸引用户

I currently have this working to grab users by username:

PrincipalContext adAuth = new PrincipalContext(ContextType.Domain, Environment.UserDomainName);

//get user
UserPrincipal usr = UserPrincipal.FindByIdentity(adAuth, username);

我一直在搜索,似乎无法找到答案来确认这是否可以完成.如果我无法使用AccountManagement进行操作,那么给定employeeID的sAMAccountName最好的方法是什么?

I have been searching and can't seem to find answers to confirm whether this can or cannot be done. If I can't do it with AccountManagement, what's the best way to get sAMAccountName given employeeID?

推荐答案

您不需要走出System.DirectoryServices.AccountManagement命名空间.

You don't need to go outside of the System.DirectoryServices.AccountManagement namespace.

UserPrincipal searchTemplate = new UserPrincipal(adAuth);
searchTemplate.EmployeeID = "employeeID";
PrincipalSearcher ps = new PrincipalSearcher(searchTemplate);

UserPrincipal user = (UserPrincipal)ps.FindOne();

在此示例中,如果未找到用户,则用户对象将为null.如果要查找UserPrinicipal对象的集合,可以在PrincipalSearcher(ps)对象上使用FindAll方法.

In this example, if no user is found the user object will be null. If you want to find a collection of UserPrinicipal object you can use the FindAll method on the PrincipalSearcher (ps) object.

还要注意,FindOne方法返回一个Principal对象,但是我们知道它实际上是一个UserPrincipal,并且应该这样处理(广播),因为UserPrincipal是搜索过滤器的一部分.

Also note that the FindOne method returns a Principal object, but we know it is really a UserPrincipal and should be handled (casted) as such since UserPrincipal is part of the search filter.

这篇关于通过employeeID获取UserPrincipal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆