I need to get the current user name in MVC 5

Viewed 233

I need to use HttpContext.Current.User.Identity.Name, but it always comes back as null. I have Anonymous Authentication set to disabled and Windows Authentication set to Enabled in the project properties. I also have the below lines in my web.config.

<authentication mode="Windows" />
<authorization>
  <deny users="?"/>
</authorization>

<providers>
....
  <add name="WindowsProvider" type="System.Web.Security.WindowsTokenRoleProvider" />
</providers>

Server settings

Every post I have looked at has suggested doing the steps I have already done, but I still have no luck.

1 Answers

Check it out with this

string userName = System.Web.HttpContext.Current.User.Identity.Name

or

User.Identity.Name
Related