Request.ServerVariables在VS 2013中不起作用

时间:2014-12-13 15:31:43

标签: c# asp.net visual-studio-2010 visual-studio

我过去几年一直在使用Visual Studio 2010,但今天我们需要更新到2013年。我去打开我现有的项目,之前没有遇到任何问题,但现在抛出以下错误:

A first chance exception of type 'System.ArgumentOutOfRangeException' 
occurred in mscorlib.dll
Additional information: startIndex cannot be larger than length of string.

此错误在此代码行中触发:

string logon_user = Request.ServerVariables["LOGON_USER"].Substring(7);

有关如何解决此问题的任何想法?经过一些研究后,我发现了一个简单的解决方法,而不是使用这一行:

string logon_user = System.Security.Principal.WindowsIdentity.GetCurrent().Name.Substring(7);

但我更感兴趣的是找出Request.ServerVariables不再工作的原因。我在很多其他项目中使用它,并希望看看它是否可以在不改变每个项目中的线条的情况下解决。

1 个答案:

答案 0 :(得分:0)

我的猜测是你在本地运行,对吗?在您的开发机器上?如果是这种情况,那么有时Visual Studio会将匿名身份验证重置为on,如果启用,则ServerVariables将返回null。

看到这个答案:

IIS express applicationhost.config security is reset every time a solutions is opened in VS2012