vb.net Request.ServerVariables("LOGON_USER") VS2010 和 VS2012

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15645329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:59:28  来源:igfitidea点击:

Request.ServerVariables("LOGON_USER") VS2010 and VS2012

asp.netvb.netvisual-studio-2010visual-studio-2012request.servervariables

提问by Nikita Silverstruk

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.

我遇到了一件对我来说很奇特的事情。我开始在 Visual Studio 2010 中开发网站,并于 2012 年完成。它是 VB.NET,框架 4.0。在整个网站中,我使用 Request.ServerVariables("LOGON_USER")。一切正常。

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!

我最近开始从头开始使用 2012 开发另一个网站。发生的情况是 Request.ServerVariables("LOGON_USER") 不返回任何值!简直是空的!但是,如果我用 Visual Studio 2010 打开这个相同的应用程序,它就可以工作!

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

谁能解释这里发生了什么以及如何在 VS2012 中修复它?谢谢!

回答by adaam

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.

出现此问题的原因是,如果您使用匿名访问安全访问 .aspx 页,则不会填充 ServerVariables 集合中与身份验证相关的变量。如果您在 Web.config 文件的部分中授予匿名用户访问权限,也会出现此问题。

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.

要在使用 None 以外的任何身份验证模式时填充 LOGON_USER 变量,您可以在 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:

只需将 Web.config 文件中的身份验证模式更改为 None 以外的任何内容。例如,Web.config 文件中的以下条目将身份验证模式设置为基于表单的身份验证:

<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>
<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!

我不确定为什么这在 VS 2010 和 2012 之间有所不同,但我以前也发生过这种情况,我使用上述步骤进行了补救。正如我所说,只需检查您的 web.config 文件!

Hope this answers your question!

希望这能回答你的问题!