问题描述
我希望能够使用该用户的显示名称在 Active Directory 中获取该用户的用户 ID.显示名称是从数据库中获取的,并在该用户的会话期间使用以下代码存储以获取显示名称:
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 (SDS.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 的所有属性都不是您真正需要的.
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 中的用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)