-1

I have been using Visual Studio 2010 for the last few years but today we were required to update to 2013. I went to open my existing project, which worked without any issues previously but now throws the following error:

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

This error is triggered at this line of code:

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

Any ideas on how to resolve this? After some research I found a simple workaround by using this line instead:

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

But I am more interested in finding out the reason why Request.ServerVariables doesn't work anymore. I am using this in tons of other projects and would like to see if it's something I can resolve without changing the line in every project.

techora
  • 619
  • 3
  • 18
  • 38
  • What is the value of `Request.ServerVariables["LOGON_USER"]` exactly? Did you debug your code? – Soner Gönül Dec 13 '14 at 15:33
  • The error says it. I think your User's Name is not more than 7 characters – Codeek Dec 13 '14 at 15:34
  • @SonerGönül The value is NULL and I can't figure out why when it pulls in my username on 2010 if I run it in that right now. – techora Dec 13 '14 at 17:20
  • @Codeek I understand that but my question is why does ServerVariables no longer work with VS 2013 and is there a work around to get it to work. – techora Dec 13 '14 at 17:28
  • We need more context. Where exactly are you making this call? Is it, for example in application_start? – Erik Funkenbusch Dec 13 '14 at 20:14

1 Answers1

0

My guess is that you're running this locally, correct? ON your development machine? If that's the case, then sometimes Visual Studio will reset Anonymous authentication to on, and if it 's on, then the ServerVariables will return null.

See this answer:

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

Community
  • 1
  • 1
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291