2012-08-06 46 views
0

我有两个使用双向信任设置的域。在活动目录域中使用User.IsInRole()检查组成员资格

域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 
} 

但不工作。

我在这里错过了什么?

+0

你是怎么指定'groupB'的?你有没有按名称指定它?还是你通过SecurityIdentifier指定了它?尝试使用SecurityIdentifier – 2012-08-07 05:12:02

+0

不完全确定你的意思。 B组是二级域(域B)上的“目标”组。组A(来自主域)嵌套在组B中。 – user1154725 2012-08-08 16:49:01

+0

“IsInRole”有多个版本。其中一个接受'string'作为参数。另一个接受'SecurityIdentifier'作为参数。 groupB的类型是什么? – 2012-08-08 21:52:04

回答

0

当在以用户身份登录且加入该域的计算机上运行时,这会失败。

 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); 
    } 
相关问题