0

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>
trailmax
  • 34,305
  • 22
  • 140
  • 234
Pirate X
  • 3,023
  • 5
  • 33
  • 60

1 Answers1

2

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;
Elim Garak
  • 1,728
  • 1
  • 16
  • 21
  • Windows Authentication was already enabled. I disabled Anonymous. No change. Still getting IIS AppPool – Pirate X Aug 03 '16 at 17:28
  • Updated the answer above.. use HttpContext.Current.User.Identity.Name; – Elim Garak Aug 04 '16 at 18:36
  • It worked ! Can you tell me what's the difference and why wasn't it working with localhost ? – Pirate X Aug 04 '16 at 18:40
  • PirateX. LocalHost (with the Studio Server is not running under windows auth) This post answers it well. http://stackoverflow.com/questions/5402249/httpcontext-current-user-principal-vs-windowsidentity-getcurrent – Elim Garak Aug 04 '16 at 18:56