Nikita Silverstruk
Nikita Silverstruk

Reputation: 1117

Request.ServerVariables("LOGON_USER") VS2010 and VS2012

I have encountered with a very peculiar for me thing. I started developing a Web site in Visual Studio 2010 and finished in 2012. It is VB.NET, framework 4.0. Throughout the Web site I use Request.ServerVariables("LOGON_USER"). Everything works as it should.

I recently started developing another Web site using 2012 from the beginning. What happens is Request.ServerVariables("LOGON_USER") does not return any value! It is simply empty! However, if I open this same application with Visual Studio 2010, it works!

Can anyone explain what is going on here and how do I fix it in VS2012? Thank you!

Upvotes: 2

Views: 6692

Answers (1)

adaam
adaam

Reputation: 3706

This problem occurs because the authentication-related variables in the ServerVariables collection are not populated if you use Anonymous Access security to access the .aspx page. This problem can also occur if you give the Anonymous user access in the section of the Web.config file.

To populate the LOGON_USER variable when you use any authentication mode other than None, you can deny access to the Anonymous user in the section of the web.config.

Just change the authentication mode in the Web.config file to anything other than None. For example, the following entry in the Web.config file sets the authentication mode to Forms-based authentication:

<authentication mode="Forms" />


<!-- To deny access to the Anonymous user in the Web.config file, use the following syntax: --!>

<authorization>
   <deny users = "?" /> <!-- This denies access to the Anonymous/unregistered user -->
   <allow users ="*" /> <!-- This allows access to all registered users -->
</authorization>

I'm not sure why this is different between VS 2010 & 2012, but this has happened to me before, and I used the above steps to remedy it. So as I said, just check your web.config file!

Hope this answers your question!

Upvotes: 2

Related Questions