c# - UserPrincipals.GetAuthorizationGroups 枚举组时发生错误 (1301)。升级到 Server 2012 域 Controller 后

标签 c# active-directory windows-server-2012 domaincontroller

研究:

Similar Issue with workaround, but not actual solution to existing problem

Similar issue pointing to Microsoft End Point update as culprit

上面的链接最适合我的问题,我在创建这篇文章时也查看了 Stack Overflow 列出的每个类似问题,只有上面引用的问题适合我的问题。

背景:

两年半以来,我一直在使用 UserPrincipal.GetAuthorizationGroups 获取在 C#.NET 4.0 Web 表单站点的 Server 2008 R2 上运行 IIS 7.5 的特定页面访问权限。 2013 年 5 月 15 日,我们删除了运行 Server 2008(不是 r2)的主域 Controller ,并将其替换为 Server 2012 域 Controller 。第二天,我们开始收到下面列出的异常。

我使用 Principal Context 进行表单例份验证。用户名/密码握手成功并且身份验证 cookie 已正确设置,但随后也调用 UserPrincipal.GetAuthorizationGroups 的 Principal Context 调用间歇性失败。我们已经解决了 Server 2012 域 Controller 中出现的一些 BPA 问题,但这还没有解决问题。我还建立了一个在两个独立服务器上运行的 cron。尽管两台服务器运行相同的代码库,但它们在不同时间会在 Group SID 解析时失败。 (开发环境和生产环境)。

该问题会在 Web 服务器重启后暂时自行解决,并且在开发服务器上也会在 12 小时未运行后自行解决。生产服务器通常会停止正常运行,直到重新启动而不自行解决。

在这一点上,我正在尝试优化针对网络中特定域 Controller 以及新 DC 的 cron,并使用目前无法产生更有针对性的异常时间的标准 LDAP 查询。到目前为止,我们已经在一台 Web 服务器上发现它发生故障的日期没有规律可循,但它会在大约 12 小时内恢复。最新结果显示 Group SID 解析在上午 8 点到晚上 8 点之间失败然后恢复,几天后它会在晚上 8 点失败并在早上 8 点恢复然后再运行 12 小时并再次失败。我们希望看看它是否只是一个特定的服务器通信问题,或者看看它是否是整套域 Controller 。

异常:

Exception information: 
Exception type: PrincipalOperationException 
Exception message: An error (1301) occurred while enumerating the groups.  
The group's SID could not be resolved.
at System.DirectoryServices.AccountManagement.SidList.TranslateSids(String target, IntPtr[] pSids)
at System.DirectoryServices.AccountManagement.SidList..ctor(SID_AND_ATTR[] sidAndAttr)
at System.DirectoryServices.AccountManagement.AuthZSet..ctor(Byte[] userSid, NetCred credentials, ContextOptions contextOptions, String flatUserAuthority, StoreCtx userStoreCtx, Object userCtxBase)
at System.DirectoryServices.AccountManagement.ADStoreCtx.GetGroupsMemberOfAZ(Principal p)
at System.DirectoryServices.AccountManagement.UserPrincipal.GetAuthorizationGroups()

问题:

鉴于上述信息,是否有人知道为什么停用 Windows Server 2008(不是 r2)并实现新的 Server 2012 DC 会导致 UserPrincipal.GetAuthorizationGroups 失败并出现 1301 SID 解析错误? 关于消除可能原因的想法也将不胜感激。

免责声明:

这是我在 Stack Overflow 上的第一篇文章,我经常在这里研究,但直到现在才加入讨论。如果我应该在其他地方发帖请原谅我,请在发帖前随时指出更好的步骤。

2013 年 6 月 13 日更新:

6 月 12 日,我解决了可能导致问题的元素未处理的可能性。 时间框架太短,无法确定调整后的代码是否已解决问题,但我会在我们努力找到解决方案时继续更新,这样也许幸运的话这里有人可以伸出援手。

原始代码

    public bool isGroupMember(string userName, ArrayList groupList)
    {
        bool valid = false;

            PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domain_server + ".domain.org:636", null, ContextOptions.Negotiate | ContextOptions.SecureSocketLayer);

            // find the user in the identity store
            UserPrincipal user =
                UserPrincipal.FindByIdentity(
                    ctx,
                    userName);

            // get the groups for the user principal and
            // store the results in a PrincipalSearchResult object
            PrincipalSearchResult<Principal> groups =
                user.GetAuthorizationGroups();

            // display the names of the groups to which the
            // user belongs
            foreach (Principal group in groups)
            {
                foreach (string groupName in groupList)
                {
                    if (group.ToString() == groupName)
                    {
                        valid = true;
                    }
                }

            }
        return valid;
    }

更新代码

        public bool isGroupMember(string userName, ArrayList groupList, string domain_server)
        {
        bool valid = false;

            try
            {

                using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domain_server + ".domain.org:636", null, ContextOptions.Negotiate | ContextOptions.SecureSocketLayer))
                {

                    // find the user in the identity store
                    UserPrincipal user =
                        UserPrincipal.FindByIdentity(
                            ctx,
                            userName);

                    try
                    {
                        // get the groups for the user principal and
                        // store the results in a PrincipalSearchResult object
                        using (PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups())
                        {
                            // display the names of the groups to which the
                            // user belongs

                            foreach (Principal group in groups)
                            {
                                foreach (string groupName in groupList)
                                {

                                    if (group.ToString() == groupName)
                                    {
                                        valid = true;
                                    }
                                }

                                group.Dispose();

                            }
                        }//end using-2
                    }
                    catch
                    {
                        log_gen("arbitrary info");
                        return false;
                    }
                }//end using-1
            }
            catch
            {
                log_gen("arbitrary info");
                return false;
            }

        return valid;

    }

最佳答案

我刚刚遇到了同样的问题,我设法找到的信息可能会有所帮助;如上所述,我们在域 Controller 运行 Server 2012 时看到了这个问题 - 首先是客户部署,然后在我们自己的网络上复制。

经过一些实验,我们发现我们的代码可以在 Server 2012 上正常运行,但当客户端系统运行 Server 2008 时会出现 1301 错误代码。有关所发生情况的关键信息可在此处找到:

MS blog translated from German

下面链接中提到的修补程序已经解决了我们测试系统上的问题

SID S-1-18-1 and SID S-1-18-2 can't be mapped

希望对大家有用!正如许多人指出的那样,此方法调用似乎相当脆弱,我们可能会在遇到其他问题之前考虑实现一些替代方法。

加里

关于c# - UserPrincipals.GetAuthorizationGroups 枚举组时发生错误 (1301)。升级到 Server 2012 域 Controller 后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17027781/

相关文章:

java - LDAP 过滤器查询如何工作,特别是在 Java LDAP/spring LDAP 中?

windows-server-2012 - 系统参数异常 : An item with the same key has already been added

c# - 元组实现

c# - 通过已知端口进行服务器间通信

c# 如何从 List<T> 创建 SortableBindingList

azure - 如何在 Windows Azure Active Directory 身份验证后获取访问 token

azure-devops - 服务连接创建操作失败

c# - 使用不带反射的字符串获取属性

php - 无法联系 ldap_bind($con, $rdn, $pwd) 上的 LDAP 服务器

c# - 在 Windows Server 2012 上将字符串从 C DLL 返回到 C# DLL 时出现 HTTP 状态 502 错误