gpt4 book ai didi

c# - 数组 : "Index was outside the bounds of the array"

转载 作者:太空狗 更新时间:2023-10-30 00:55:08 24 4
gpt4 key购买 nike

我的 Web 应用程序出现以下错误:“索引超出数组范围。”

奇怪的是,问题只出现在我的网络服务器上(Windows Server 2008 R2、.Net 4.0、Windows 7 Enterprise 64 位)。

当我在 Visual Studio 中调试站点时,它一切正常

代码如下所示:

我定义了一个数组并将其交给“CheckUserRights”类

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);

这个类看起来像这样:

//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();
}
...
...

我在 4 小时后搜索了解决方案,但我找不到任何东西。我不是专业人士,这是我第一次使用数组。

会不会是服务器配置错误?

我很感激你的每一次帮助。谢谢!

更新

已解决,服务器配置有问题。解决方案在史蒂夫的回答中。

最佳答案

该代码中有很多问题,但是,坚持问题标题上的问题,我认为这一行看起来很可疑:

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

如果 Page.User.Identity.Name 不包含 DOMAIN\USERNAME 会怎样?

可以重写为

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;

为什么这个属性可能是错误的? Look at this article

关于c# - 数组 : "Index was outside the bounds of the array",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10499670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com