IndexOutOfRangeException:索引超出了Pro服务器中数组的界限

本文关键字:服务器 数组 界限 Pro 索引 IndexOutOfRangeException | 更新日期: 2023-09-27 18:27:06

我得到以下错误IndexOutOfRangeException:从Active Directory调用电子邮件id时,索引超出了数组的界限,这是代码,并遵循错误

string propertyName = "mail";
string User = HttpContext.Current.User.Identity.Name;
string[] Name = Regex.Split(User.Trim(), @"''");
string username = Name[1];
//string domainname = HttpReq//System.Environment.UserDomainName.ToString().ToLower();
string domainname = Name[0]; //"AsiaPacific";
DirectoryEntry entry = new DirectoryEntry("LDAP://DC=" + domainname + ",DC=cpqcorp,DC=net");
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(&(&(objectClass=user)(SamAccountName=" + username + ")))";
search.PropertiesToLoad.Add(propertyName);
SearchResult result = search.FindOne();
string propertyValue = "";
if (result != null)
{
    propertyValue = result.Properties[propertyName][0].ToString();
    //propertyValue = result.Properties.Count.ToString();
}
return propertyValue;

异常错误:

[IndexOutOfRangeException:索引超出了数组的界限。]clsCommon.DisplayName()

当我在Pro服务器中运行该工具时会发生这种情况,如果我运行本地,则不会出现这种情况。

IndexOutOfRangeException:索引超出了Pro服务器中数组的界限

解决了我自己的问题,将Pro服务器中的身份验证从匿名更改为Windows身份验证。感谢回复