Sunday, March 25, 2012

Can we access Active Directory Group in a report?

Is there a way that we can tell what active directory group the person belongs to that is running the report? I know that you can detect a user id, but I need to access the Active Directory Group that they belong to.

I'm not sure if there is another method, but this is the method I used for checking if a user belongs to a particular group. It can be altered to return the group:

Added to the code window.

Code Snippet

Function IsMemberOfGroup(ByVal UserName As String, ByVal GroupName As String) As Boolean
Try
Dim ent As System.DirectoryServices.DirectoryEntry = New System.DirectoryServices.DirectoryEntry("LDAP://DC=domainname,DC=com")

Dim srch As System.DirectoryServices.DirectorySearcher = New System.DirectoryServices.DirectorySearcher("(CN=" + GroupName + ")")
Dim coll As System.DirectoryServices.SearchResultCollection = srch.FindAll()
For Each rs As System.DirectoryServices.SearchResult In coll
Dim resultPropColl As System.DirectoryServices.ResultPropertyCollection = rs.Properties
For Each memberColl As Object In resultPropColl("member")
Dim gpMemberEntry As System.DirectoryServices.DirectoryEntry = New System.DirectoryServices.DirectoryEntry("LDAP://" + memberColl.ToString)
Dim userProps As System.DirectoryServices.PropertyCollection = gpMemberEntry.Properties
Dim obVal As Object = userProps("sAMAccountName").Value
If obVal.ToString = UserName Then Return True
Next
Next
Catch ex As Exception
'Trace.Write(ex.Message)
End Try
Return False
End Function

No comments:

Post a Comment