【问题标题】:using User.IsInRole() across active directory domains to check for group membership跨活动目录域使用 User.IsInRole() 来检查组成员身份
【发布时间】:2012-08-03 21:39:57
【问题描述】:

我有两个使用双向信任设置的域。

域 A 有一个组(组 A)和一个成员(用户 A)。

域 B 有一个组(组 B),组 A(来自其他域)作为成员。

我正在检查:

if(User.IsInRole(group B))
{
  // logging in as User A should provide access because this use is part of Group A which is part of Group B
}

但这不起作用。

我在这里错过了什么?

【问题讨论】:

  • 你是如何指定groupB的?您是否按名称指定了它?还是您通过 SecurityIdentifier 指定了它?尝试使用 SecurityIdentifier
  • 不完全确定您的意思。组 B 是辅助域(域 B)上的“目标”组。 A 组(来自主域)嵌套在 B 组中。
  • IsInRole 有多个版本。其中之一接受string 作为参数。另一个接受SecurityIdentifier 作为参数。 groupB 的类型是什么?
  • 啊好吧...我传入一个字符串。如何获取要传入的 SID?
  • 你可以得到GroupPrincipal,然后得到它的Sid

标签: windows active-directory windows-server-2008 trust


【解决方案1】:

在以用户身份登录并加入该域的计算机上运行时,这对我来说失败了。

        private static SecurityIdentifier GetGroupSid(string domainName, string groupName)
    {
        using (var d = Domain.GetDomain(new DirectoryContext(DirectoryContextType.Domain, domainName)))
        {
            using (var context = new PrincipalContext(ContextType.Domain, d.Name))
            {
                using (var group = GroupPrincipal.FindByIdentity(context, groupName))
                {
                    return group.Sid;
                }
            }
        }
    }
    [Test]
    public void should_check_role_with_sid()
    {

        var barDomain = "bar.example.com";
        var groupinBar = GetGroupSid(barDomain, "group_in_bar");
        var identity = WindowsIdentity.GetCurrent();
        var windowsPrincipal = new WindowsPrincipal(identity);
        Assert.That(windowsPrincipal.IsInRole(groupinBar), Is.True, "Checking role " + groupinBar);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多