Pirate X Pirate X - 4 months ago 25
ASP.NET (C#) Question

IIS not picking up Windows User

I made a web application which displays Windows User Name on top right.
If I open the webapp on

localhost:5903/index.aspx
I get correct Windows User.
Like this -



enter image description here

But if I open
localhost/db/index.aspx
I get IIS AppPool User -




enter image description here

Here db is the directory which has my app.

I have no idea why is this happening. I want the localhost to show the correct Username as well.

My IIS tree, here DB(1) is my webapp -

enter image description here

My C# code which picks Windows Identity -

<li><% string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;%>
<a><%= userName %></a>
</li>

Answer

You need to setup the site in IIS for Windows Authentication only and turn off Anonymous.

Go into IIS for you site then under Features View.

Then under the IIS category > Authentication > Set Windows Authentication to Enabled and all others (Anonymous....others) to Disabled.

Instead of...

System.Security.Principal.WindowsIdentity.GetCurrent().Name;

Use

 HttpContext.Current.User.Identity.Name;