我只需要显示我正在使用的 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/