【问题标题】:Get User name in stead of DomainName\UserID in C#在 C# 中获取用户名而不是域名\用户 ID
【发布时间】:2018-02-28 08:54:04
【问题描述】:
我正在使用User.Identity.Name 获取用户名。
但我得到的是 DomainName\UserID 而不是用户名。
我正在使用 Windows 身份验证。
任何人都可以建议任何方法在 Windows 身份验证中获取用户名而不是用户 ID。 C#。
谢谢……
【问题讨论】:
标签:
c#
asp.net
windows-authentication
【解决方案1】:
我认为您正在寻找用户显示名称。这是获得它的方法:
using System.DirectoryServices.AccountManagement;
var name = UserPrincipal.Current.DisplayName;
您需要添加对System.DirectoryServices.AccountManagement.dll的引用
【解决方案2】:
logon_user = Request.ServerVariables["LOGON_USER"].Substring(// you domain length here);
例如:
如果 Domain 的长度是“8”个字符,那么
logon_user = Request.ServerVariables["LOGON_USER"].Substring(8);