2008-12-16 14 views
3

J'essaie de renforcer mon code qui détermine si un utilisateur est membre d'un groupe AD donné. Il fonctionne essentiellement sauf lorsque le membre du groupe provient d'un autre domaine (de confiance) car il est stocké en tant que foreignsecurityprincipal. Etant donné que j'ai un objet DirectoryEntry valide pour le groupe que je veux tester et le compte que je veux vérifier, j'ai besoin d'une chaîne de filtrage DirectorySearcher qui me permettra de confirmer que le compte est dans ce groupe, même si le compte est un foreignsecurityprincipal.Comment puis-je déterminer si un groupe AD contient un DirectoryEntry donné d'un autre domaine (de confiance)?

(code VB.NET Exemple démontrant la question)

Dim ContainerGroup as DirectoryEntry = ... Code to get Group 
Dim UserToCheckFor as DirectoryEntry = ... Code to get User 

DSearcher = New DirectorySearcher(ContainerGroup, "(WHATCANIPUTINHERE)", New String() {"member;Range=0-5000"}, SearchScope.Base) 
DSearcher.AttributeScopeQuery = "member" 

'If an object is found, the account was in the group 
Return (DSearcher.FindOne() IsNot Nothing) 

Répondre

1

D'accord. Je l'ai trouvé. Voici l'astuce.

J'essaie de renforcer mon code qui détermine si un utilisateur est membre d'un groupe AD donné. Il fonctionne essentiellement sauf lorsque le membre du groupe provient d'un autre domaine (de confiance) car il est stocké en tant que foreignsecurityprincipal.

(Code Echantillon VB.NET)

Dim ContainerGroup as DirectoryEntry = ... Code to get Group 
Dim UserToCheckFor as DirectoryEntry = ... Code to get User 

DSearcher = New DirectorySearcher 
Dim DSearcher As New DirectorySearcher(ContainerGroup, getLDAPQueryStringUsingSID(containedGroup), New String() {"member;Range=0-5000"}, SearchScope.Base) 

Return (DSearcher.FindOne() IsNot Nothing) 


** Helper Methods ** 

Private Function getLDAPQueryStringUsingSID(ByVal DEObject As DirectoryEntry) As String    
    Return "(objectSid=" + getSDDLSidForDirectoryEntry(DEObject) + ")" 
End Function 

Private Function getSDDLSidForDirectoryEntry(ByVal DEObject As DirectoryEntry) As String 
     Dim bytes As Byte() = CType(DEObject.Properties("objectSid").Value, Byte()) 
     Dim sid As New System.Security.Principal.SecurityIdentifier(bytes, 0) 
     Return sid.ToString 
End Function