GetAuthorizationGroups throws索引超出了数组SOMETIMES的范围

时间:2013-09-18 16:26:26

标签: c# iis active-directory active-directory-group

以下是使用的代码。 “44号线”是问题所在:

var groups = p.GetAuthorizationGroups();

在正常运行一天后,它会出现Index was outside the bounds of the array错误。最后一点似乎是这种情况的真正嘘声。我发现其他GetAuthorizationGroups抛出越界错误已经解决了很多问题,但我找不到任何关于MOSTLY工作的系统的解决方案。

任何和所有帮助都将非常感激。我会尽力纠正这个帖子的任何问题。

编辑:目前清除错误的唯一方法是重启网络服务器。只需重新启动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"];
}

0 个答案:

没有答案