Public Class PRmain Inherits System.Web.UI.Page 'To find login username and domain Dim domain As String = Me.Page.User.Identity.Name.Split("\".ToCharArray())(0) Dim username As String = Me.Page.User.Identity.Name.Split("\".ToCharArray())(1) 'Find if the user is in a group Public Function IsInGroup(ByVal context As HttpContext, ByVal group As String) As Boolean Return context.User.IsInRole(group) End Function 'Find all groups the user belongs to Public Function GetUserMemberships() As ArrayList Dim results As ArrayList = New ArrayList() Dim myDE As New System.DirectoryServices.DirectoryEntry("LDAP://mysite.com") Dim mySearcher As New DirectorySearcher(myDE) mySearcher.Filter = "sAMAccountName=" & username mySearcher.PropertiesToLoad.Add("memberOf") Dim propertyCount As Integer Try Dim myresult As SearchResult = mySearcher.FindOne() propertyCount = myresult.Properties("memberOf").Count Dim dn As String Dim commaIndex As Integer Dim equalsIndex As Integer For i As Integer = 0 To propertyCount - 1 Try dn = myresult.Properties("memberOf")(i).ToString equalsIndex = dn.IndexOf("=", 1) commaIndex = dn.IndexOf(",", 1) results.Add(Trim(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1)).ToUpper()) Catch ex As Exception ' there was an error, this membership will not be included End Try Next Catch ex As Exception 'they are still a good user just does not 'have a "memberOf" attribute so it errors out. End Try Return results End Function End Class
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)