HttpContext.Current.User.Identity.Name returns blank

Andy5 picture Andy5 · Nov 14, 2012 · Viewed 49.3k times · Source

I am using HttpContext.Current.User.Identity.Name to get the user name when the web application is in use. During development I was using my local iis, with integrated windows authentication enabled and anonymous access enabled and disabled, and I was able to get the username.

Now, when I publish the web application, it comes back blank. The setup on the published server is the same, and I have tried using Page.User.Identity.Name, which also returned blank.

Does anyone know why this is and how to fix it?

Answer

Thing picture Thing · Nov 22, 2013

You probably had Anonymous Authentication on as well as Windows Authentication. Turn off Anonymous off.

So,

<system.web>
    <authentication mode="Windows" />
</system.web>

In IIS config for the app, look in Authentication tab

Set **Anonymous Authentication** to **Disabled** and
Set **Windows Authentication** to **Enabled**

This should work and the Identity.UserName should now show up properly.