c# - 从 Active Directory 获取用户名

标签 c# asp.net active-directory

我只需要显示我正在使用的 Active Directory 中的用户名

 lbl_Login.Text = User.Identity.Name; //the result is domain\username

这里显示的是用户名而不是用户的真实姓名,我已经检查了这里相关的其他问题和答案,但我没有得到解决方案。

有没有像“User.Identity.Name”这样的属性可以只获取用户的名字?

最佳答案

您需要事件目录中的用户名。试试这样的代码:

string name ="";
using (var context = new PrincipalContext(ContextType.Domain))
{
    var usr = UserPrincipal.FindByIdentity(context, User.Identity.Name); 
    if (usr != null)
       name = usr.DisplayName;  
}

或者这个来自social.msdn.microsoft.com :

PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
UserPrincipal user = UserPrincipal.Current;
string displayName = user.DisplayName;

或者可能是:

System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName;

The System.DirectoryServices.AccountManagement namespace provides uniform access and manipulation of user, computer, and group security principals across the multiple principal stores: Active Directory Domain Services (AD DS), Active Directory Lightweight Directory Services (AD LDS), and Machine SAM (MSAM).

关于c# - 从 Active Directory 获取用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39215056/

相关文章:

c# - 根据 ViewModel 的属性更改 div 类

c# - GridView 中 DropDownList 的动态名称

c# - 通过 c#/.Net 从 AD 检索特定用户详细信息

powershell - 获取广告商制作属性列表

c# - 是否可以将反射访问器缓存到支持字段以进行优化?

由于名称相同或语法错误而导致 C# 错误?

asp.net - Javascript结果生成Jquery CRUD ajax操作

c# - 放弃 AJAX Control Toolkit 2013 年 7 月版中的组合

node.js - 如何使用 NodeJS 登录 Azure AD?

c# - 由于其保护级别,公共(public)无效方法无法访问