获取当前登录的用户名

时间:2014-08-01 12:45:57

标签: asp.net-mvc web-applications directoryservices windows-identity

我想获取当前登录用户的用户名。我使用下面的代码,但它返回其凭据在IIS应用程序池标识中使用的用户。 mvc网站部署在服务器上。

我用过

string userid = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

string userid = Environment.UserName;

string userid = System.DirectoryServices.AccountManagement.UserPrincipal.Current.GivenName.ToString();

string userid = System.Web.HttpContext.Current.User.Identity.Name.ToString()

但似乎没有给我正确答案。

我需要在HomeController的构造函数中获取用户名。

以下是我的web.config

<authentication mode="Windows" />
<authorization>
    <allow users="*" />
</authorization>

1 个答案:

答案 0 :(得分:1)

而不是

string userid = System.Web.HttpContext.Current.User.Identity.Name.ToString()

使用它,这将正常工作

string userid = HttpContext.Current.User.Identity.Name.ToString()