微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 为什么Principal.IsMemberOf()对某些组返回false negative?

为什么Principal.IsMemberOf(GroupPrincipal)( MSDN)在以下测试中返回Domain Computers组的假阴性?

[TestMethod]
public void DomainComputertest()
{
    var distinguishedname = "CN=MyMachine,DC=SomeDomain,DC=local";
    using( var pc = new PrincipalContext( ContextType.Domain,"SomeDomain.local","UserName","Password" ) )
    using( var computer = ComputerPrincipal.FindByIdentity( pc,IdentityType.distinguishedname,distinguishedname ) )
    {
        Assert.IsNotNull( computer );
        // Get the groups for the computer.
        var groups = computer.GetGroups().Cast<GroupPrincipal>();
        foreach( var group in groups )
        {
            // Immediately turn around and test that the computer is a member of the groups it returned.
            Assert.IsTrue( computer.IsMemberOf( group ),"Computer is not member of group {0}",group.Name );
        }
    }
}

结果消息:Assert.IsTrue失败.计算机不是“域计算机”组的成员

计算机确实是“域计算机”组的成员,“GetGroups()”方法正确返回.实际上,如果您尝试将计算机添加到组中,则会抛出PrincipalExistsException.

我可以与用户和“域用户”组重现完全相同的行为.这是因为这些团体是校长团体吗?是因为这些是“认”群组吗?

编辑添加:我们正在使用.NET 4.5.1.

解决方法

这似乎是一直存在的问题.

我在同一个问题上发现的每个问题都没有答案.我找不到任何说这是作为bug提交的内容,但似乎自.NET 3.5以来就存在.

我试图让你的例子返回true(当然改变了我的工作领域的信息).无论如何,它都是假的.在dotpeek中反编译Principal类仅得出推测.完成并设置Visual Studio以允许步入.NET框架代码一个破坏,因为它不会进入必要的方法.我可以进入其他.NET框架代码,但在Principal上没有.不确定这是否与使用SecurityCriticalAttribute标记方法有关.愿意确认.

我的建议是将此文件作为错误提交,并在同一时间采用不同的路径来确定计算机是否为成员.在我的测试中,group.Members确实包含了计算机.

我遇到了运行.NET 3.5,4.0,4.5,4.5.1的相同问题

以下是一些参考:

解决方法GroupPrincipal.IsMemberOf always returns false

A 2010 MSDN Blog entry with a comment that had the same issue as you.

注意:我通常不会这样回答,但因为我在这个问题上发现的每个问题都有0个答案或解决方法,我认为对于未来的读者来说,实际上看到这个问题的某种“答案”是有益的.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐