加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 02:03:51 所属栏目:百科 来源:网络整理
导读:为什么Principal.IsMemberOf(GroupPrincipal)( MSDN)在以下测试中返回Domain Computers组的假阴性? [TestMethod]public void DomainComputerTest(){ var distinguishedName = "CN=MyMachine,DC=SomeDomain,DC=local"; using( var pc = new PrincipalContext
为什么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个答案或解决方法,我认为对于未来的读者来说,实际上看到这个问题的某种“答案”是有益的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读