IndexOutOfRangeException:索引超出了Pro服务器中数组的范围

时间:2014-10-20 14:42:40

标签: c#

我收到以下错误 IndexOutOfRangeException:当从Active Directory调用Email 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服务器中运行该工具时,如果我在本地运行,则会发生这种情况。

1 个答案:

答案 0 :(得分:-1)

解决了自己,在Pro服务器中更改了身份验证,从匿名更改为Windows身份验证。谢谢你的回复