gpt4 book ai didi

c# - 为什么 Principal.IsMemberOf() 对某些组返回假阴性?

转载 作者:行者123 更新时间:2023-11-30 13:45:43 24 4
gpt4 key购买 nike

为什么 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。

最佳答案

这似乎是一个一直存在的问题。

我发现的与您相同的问题的每个问题都没有得到解答。我找不到任何说这是作为错误提交的内容,但似乎自 .NET 3.5 以来就存在了。

我试图让您的示例返回 true(当然将信息更改为我的工作域)。不管怎样,它返回了 false。反编译 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 个答案或解决方法,我认为这对 future 的读者实际看到某种“回答”这个问题。

关于c# - 为什么 Principal.IsMemberOf() 对某些组返回假阴性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26981922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com