如何从C#中的显示名称获得Active Directory中的用户名? [英] How to get a username in Active Directory from a display name in C#?
问题描述
我希望能够获得使用该用户的显示名称在Active Directory中的用户的用户ID。显示名称是从数据库得到的,并使用以下code键获得的显示名称该用户的会话期间已被存储:
I want to be able to obtain the userid of a user in Active Directory using the display name of that user. The display name is obtained from a database, and has been stored during that user's session using the following code to obtain the display name:
using System.DirectoryServices.AccountManagement;
private string GetDisplayName()
{
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// find currently logged in user
UserPrincipal user = UserPrincipal.Current;
return user.DisplayName;
}
这一次,我想有一个名为方法 GetUserIdFromDisplayName()
返回Active Directory中的登录名。任何想法?
This time around, I would like to have a method named GetUserIdFromDisplayName()
that returns the Active Directory login name. Any ideas?
推荐答案
我相信你可以用更轻松地做到这一点比大卫的回答内置的功能 System.DirectoryServices.AccountManagement
(S.DS.AM)命名空间。
I believe you can do it much more easily than with David's answer by using the built-in functionality of the System.DirectoryServices.AccountManagement
(S.DS.AM) namespace.
基本上,你可以定义域范围内,并很容易地找到在AD用户和/或组:
Basically, you can define a domain context and easily find users and/or groups in AD:
using System.DirectoryServices.AccountManagement;
private string GetUserIdFromDisplayName(string displayName)
{
// set up domain context
using(PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
// find user by display name
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, displayName);
//
if (user != null)
{
return user.SamAccountName;
// or maybe you need user.UserPrincipalName;
}
else
{
return string.Empty;
}
}
}
我看不出有什么必要去底层的DirectoryEntry
的对象,真的 - 除非没有任何 UserPrincipal $的属性C $ C>真的是你要找的内容。
I don't see any need to go to the underlying DirectoryEntry
object, really - unless none of the properties of the UserPrincipal
really are what you're looking for.
PS:如果按显示名称查找不应该工作(我没有一个AD手头现在来测试它) - 你总是可以同时使用 PrincipalSearcher
找到你的用户:
PS: if the search by display name shouldn't work (I don't have an AD at hand to test it right now) - you can always also use the PrincipalSearcher
to find your user:
using System.DirectoryServices.AccountManagement;
private string GetUserIdFromDisplayName(string displayName)
{
// set up domain context
using(PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
// define a "query-by-example" principal - here, we search for a UserPrincipal
// and with the display name passed in
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.DisplayName = displayName;
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find match - if exists
UserPrincipal user = srch.FindOne() as UserPrincipal;
if (user != null)
{
return user.SamAccountName;
// or maybe you need user.UserPrincipalName;
}
else
{
return string.Empty;
}
}
}
这篇关于如何从C#中的显示名称获得Active Directory中的用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!