2014-10-20 32 views
-5

我收到低于错误 IndexOutOfRangeException:索引在调用Active Directory中的电子邮件ID时位于数组边界之外,此处为代码并出现错误IndexOutOfRangeException:索引超出了Pro服务器中数组边界的范围

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: Index was outside the bounds of the array.] clsCommon.DisplayName()

当我运行在Pro服务器不进来,如果我运行本地的工具发生这种情况。

+0

你的'Split'正在返回一个单独的元素,它看起来在你的字符串中没有双斜杠,可能是你试图在单个反斜杠上分割,使用''\\“'或' @“\”', – Habib 2014-10-20 14:45:45

+0

'SearchResult'也可能是问题 – 2014-10-20 14:47:31

+0

“Name”只有一个元素,或者“Properties [propertyName]”有零个元素。使用调试器来确定。 – 2014-10-20 14:48:34

回答

0

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

相关问题