Array: "The index was outside the array"

The following error appears in my web application: "The index was outside the array."

It is strange that the problem only occurs on my web server (Windows Server 2008 R2, .Net 4.0, Windows 7 Enterprise 64 Bit).

When I debug a site in Visual Studio everything works fine

The code is as follows:

I define an array and pass it to the "CheckUserRights" class

string NTLogin = Page.User.Identity.Name.ToString().Split(new char[] { '\\' })[1].ToUpper(); string[] allowedRoles = new string[2] { "Administrator", "Superuser" }; CheckUserRights Check = new CheckUserRights(NTLogin, allowedRoles); 

The class is as follows:

 //Lokale Variablen definieren string strUserName; string strRolename; string[] AllowedRoles; bool boolAuthorized; //Im Konstruktor definierte Werte รผbergeben. public CheckUserRights(string Username, string[] Roles) { this.strUserName = Username; this.AllowedRoles = Roles; getRoleForUser(); checkRights(); } ... ... 

I searched for a solution after 4 hours, but cannot find anything. I am not a professional, and I first used arrays.

Could there be a wrong configuration on the server?

I am grateful for any help. Thanks!

Update

Solved, there was a problem in the server configuration. The solution is in response from Steve.

+4
source share
3 answers

There are a lot of problems in this code, but sticking to the problem in the question title, I think this line looks suspicious:

 string NTLogin = Page.User.Identity.Name.ToString().Split(new char[] { '\\' })[1].ToUpper(); 

What happens if the page_name .Iser.Identity.Name does not contain DOMAIN \ USERNAME?

May be rewritten as

 string[] nameparts = Page.User.Identity.Name.ToString().Split(new char[] { '\\' }); string NTLogin = (nameparts.Length == 2 ? nameparts[1] : nameparts[0]).ToUpper(); if(NTLogin.Length == 0) return; 

Why might this property be a mistake? Take a look at this article.

+5
source

1) Your code is vulnerable to SQL injection attack on first choice in getRoleForUser. You must use SqlParameter.

2) without a stack trace, this makes it difficult to determine where the error is. However, I think it is possible that sql-selects can return NO ROWS. So, whenever you do r.Read , change it to if (!r.Read()) return; .

Again, if you place a stack trace, we can help.

+1
source

As I decided, there was the following:

 Dim strname() As String For indx = 0 To (strname.Length - 1) If strname(indx) = "" Then Else ListBox1.Items.Add(strname(indx)) End If Next 
0
source

Source: https://habr.com/ru/post/1411314/


All Articles