DirectoryServices

vb.net , directoryservices , asp.net Miami, United States
  • 6 years ago

    Hi

    I have a DropDown, If user select any item from list, then some labels , grid on my asp.net page will populate data from sql database.

    but my problem is i want to display data based on the user who is log in.

    For validating user, i am using directoryservices.

    My code to validate login users is below for reference.

    Let me know using this code how can i display data in label/grid based on who is log in.

    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

    Public Class ProductionRecordmain 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://dadeschools.net") 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 member

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Before software should be reusable, it should be usable.” - Ralph Johnson