2012-08-06 52 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); 
    } 
相關問題