下面是使用的代码。“第 44 行”是问题区域:
var groups = p.GetAuthorizationGroups();
Index was outside the bounds of the array
在正常运行大约一天后,它给了我一个错误。最后一点似乎是这种情况的真正鼻屎。我发现了很多其他GetAuthorizationGroups
抛出越界错误的已解决问题,但我找不到任何关于最有效的系统的解决方案。
任何和所有的帮助将不胜感激。我会尽我所能纠正这个帖子的任何问题。
编辑:目前清除错误的唯一方法是重新启动网络服务器。简单地重新启动 IIS 并不能解决问题。
public override String[] GetRolesForUser(String username)
{
//Check to see if role are already cached in Session variable
if ((string[])System.Web.HttpContext.Current.Session["Roles"] == null)
{
//Create an ArrayList to store our resultant list of groups.
ArrayList results = new ArrayList();
//PrincipalContext encapsulates the server or domain against which all operations are performed.
using (PrincipalContext context = new PrincipalContext(ContextType.Domain, null, "blargh.com"))
{
try
{
//Create a referance to the user account we are querying against.
UserPrincipal p = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username);
//Get the user's security groups. This is necessary to
//return nested groups, but will NOT return distribution groups.
var groups = p.GetAuthorizationGroups();
foreach (GroupPrincipal group in groups)
{
results.Add(group.SamAccountName);
}
}
catch (Exception ex)
{
throw new ProviderException("Unable to query Active Directory.", ex);
}
}
// Store roles in Session for caching
System.Web.HttpContext.Current.Session["Roles"] = results.ToArray(typeof(String)) as String[];
}
return (string[])System.Web.HttpContext.Current.Session["Roles"];
}