I am trying to build an application on our company\'s intranet using ASP.NET and VB.NET.
Neither of these functions return anything once my application is published
Here's what I found (somewhere), and ended up using. Hope it can help someone else out there!
Public Shared Function Check_If_Member_Of_AD_Group(ByVal username As String, _
ByVal grouptoCheck As String, _
ByVal domain As String, _
ByVal ADlogin As String, _
ByVal ADpassword As String) _
As Boolean
Dim myDE As DirectoryEntry
Dim EntryString As String
Dim NumberOfGroups As Integer
Dim tempString As String
'Checks to see if the specified user is a member of the specified group
Try
'Setup the LDAP basic entry string.
EntryString = "LDAP://" & domain
'Make the group to check all lowercase (for matching)
grouptoCheck = grouptoCheck.ToLower()
'Use the correct overloaded function of DirectoryEntry
If (ADlogin <> "" AndAlso ADpassword <> "") Then
myDE = New DirectoryEntry(EntryString, ADlogin, ADpassword)
Else
myDE = New DirectoryEntry(EntryString)
End If
'Filter the directory searcher and get the group names
Dim myDirectorySearcher As New DirectorySearcher(myDE)
myDirectorySearcher.Filter = "sAMAccountName=" & username
myDirectorySearcher.PropertiesToLoad.Add("MemberOf")
Dim myresult As SearchResult = myDirectorySearcher.FindOne()
'Get the number of groups, so they can be itereated
NumberOfGroups = myresult.Properties("memberOf").Count() - 1
While (NumberOfGroups >= 0)
'Extract the group name from the result set of the index
tempString = myresult.Properties("MemberOf").Item(NumberOfGroups)
tempString = tempString.Substring(0, tempString.IndexOf(",", 0))
tempString = tempString.Replace("CN=", "")
tempString = tempString.ToLower()
tempString = tempString.Trim()
If (grouptoCheck = tempString) Then 'We got a winner
Return True
End If
NumberOfGroups = NumberOfGroups - 1
End While
Return False 'User is not in the specified group
Catch ex As Exception
Check_If_Member_Of_AD_Group = False 'If all else fails, don't authenticate
End Try
End Function