在C#中获取用户名而不是DomainName \ UserID

时间:2018-02-28 08:54:05

标签: c# asp.net windows-authentication

我使用User.Identity.Name获取用户名。

但是我取代了用户名,而是获得了DomainName \ UserID。

我正在使用Windows身份验证。

任何人都可以建议以任何方式在Windows身份验证中获取用户名的UserID。 C#。

谢谢....

2 个答案:

答案 0 :(得分:0)

我认为您正在寻找用户显示名称。这是获得它的方法:

using System.DirectoryServices.AccountManagement;
var name = UserPrincipal.Current.DisplayName;

您需要添加对System.DirectoryServices.AccountManagement.dll

的引用

答案 1 :(得分:-1)

logon_user = Request.ServerVariables["LOGON_USER"].Substring(// you domain length here);

例如: 如果Domain长度为“8”字符,

logon_user = Request.ServerVariables["LOGON_USER"].Substring(8);