Why does HttpContext.Current.User.Identity.Name return blank

I created a site. It is an internal site. It is a .NET 4.0 site. I would like to grab the username using HttpContext.Current.User.Identity.Name of the person browsing the site. There isnt a login page since it is internal. I am not sure what I am missing. Do I need the following in my webconfig:

<authentication mode="Windows"></authentication>
<identity impersonate="true"/>
<authorization>
  <allow users="?"/>
</authorization>

and this:
  <system.webServer>
  <validation validateIntegratedModeConfiguration="false" />

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

It comes up blank when unauthenticated. You can verify this by also doing:

HttpContext.Current.User.Identity.IsAuthenticated

Check your authentication level in IIS, is it set to enable windows authentication there too?

Method 2

In iis disable anonymous access and enable integrated windows security for you web application.

Method 3

Yes, you do need, at least the

<authentication mode="Windows"></authentication>

part is required if you are using Integrated Windows Auth.

After that, you should be able to grab the username of the person logged in.

No, you don’t need:

<validation validateIntegratedModeConfiguration="false" />

Method 4

In IIS, go to Sites Default Website [Your Website].

Select Authentication option and disable Anonymous Authentication.

Method 5

In my case removing <remove name="FormsAuthentication" /> line from web.config helps.

I also have <authentication mode="Forms" /> in <system.web> section.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x