2017-09-06 13 views
0

をユーザーのグループメンバーシップを検索し、私は、特定のユーザが直接VB.NETは(間接的に)再帰的に

Public Function IsInGroup(ByVal username As String, ByVal password As String) As Collection 
    Dim Groups As New Collection 
    Dim domain = "registry" 
    Dim dirEntry As New DirectoryEntry("LDAP://" & domain, username, password, DirectoryServices.AuthenticationTypes.Secure) 
    Dim dirSearcher As New DirectorySearcher(dirEntry) 
    dirSearcher.Filter = "(SAMAccountName=" + username + ")" 
    dirSearcher.PropertiesToLoad.Add("memberOf") 
    Dim propCount As Integer 
    Try 
     Dim dirSearchResults As SearchResult = dirSearcher.FindOne() 
     propCount = dirSearchResults.Properties("memberOf").Count 
     Dim dn As String 
     Dim equalsIndex As String 
     Dim commaIndex As String 
     For i As Integer = 0 To propCount - 1 
      dn = dirSearchResults.Properties("memberOf")(i) 
      equalsIndex = dn.IndexOf("=", 1) 
      commaIndex = dn.IndexOf(",", 1) 
      If equalsIndex = -1 Then 
       Return Nothing 
      End If 
      If Not Groups.Contains(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1)) Then 
       Groups.Add(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1)) 
      End If 
     Next 
    Catch ex As Exception 
     If ex.GetType Is GetType(System.NullReferenceException) Then 
      MessageBox.Show("Selected user isn't a member of any groups at this time.", "No groups listed", MessageBoxButtons.OK, MessageBoxIcon.Error) 
      'they are still a good user just does not 
      'have a "memberOf" attribute so it errors out. 
      'code to do something else here if you want 
     Else 
      MessageBox.Show(ex.Message.ToString, "Search Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
     End If 
    End Try 
    'Console.WriteLine(Groups) 
    Return Groups 

End Function 

のメンバーであるグループを取得するには、以下のコードを使用しています。しかし、どのように私は、そのユーザグループを取得しますInDirectlyのメンバーですか?

アイデア?

答えて

0

再帰的なグループメンバシップを照会して列挙するのではなく、tokenGroups属性を照会することで、Active Directoryでこれを行う必要があります。

tokenGroups属性は、Active Directoryによって計算されたSIDの配列で、ユーザーアクセスを確認するために使用されます。

実際のグループ名を取得するには、これらのSIDをsAMAccountNamesに変換する必要があります。

アンマネージドコードでは、これはDsCrackNames APIまたはIADsNameTranslateインターフェイスを呼び出すことで実現できます。

(VB).NETで最も簡単な方法は、GetAuthorizationGroupsメソッドを公開するUserPrincipalクラス(.NET Framework 3.5以降が必要)を使用することです。

たとえば、https://www.remkoweijnen.nl/blog/2011/01/18/recursive-group-membership-in-powershell/を参照してください。それはPowerShellにありますが、VB.NETに変換するのは簡単です。

+0

ありがとうございます - 私は見ています、もしtokenGroupsがより速く、より良い私に良い音:) –