CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #1
    Join Date
    May 1999
    Posts
    226

    [RESOLVED] LDAP Authentication Difficulties

    Code:
        Public Function Authenticate(ByVal username As String, ByVal password As String) As Boolean
    
            Dim path As String = "LDAP://dc=" + Environment.UserDomainName
            Dim domainAndUsername As String = username + "@" + Environment.UserDomainName
            Dim entry As DirectoryEntry = New DirectoryEntry(path, domainAndUsername, password)
    
            Try
                ' Bind to the native AdsObject to force authentication.
                Dim obj As Object = entry.NativeObject
                Dim search As DirectorySearcher = New DirectorySearcher(entry)
                search.Filter = "(SAMAccountName=" + username + ")"
                search.PropertiesToLoad.Add("cn")
                Dim result As SearchResult = search.FindOne()
                If result Is Nothing Then
                    Return False
                End If
                ' Update the new path to the user in the directory
                path = result.Path
                Dim filterAttributeas As Object = result.Properties("cn")(0)
            Catch ex As Exception
                lblMessage.Text = "Error authenticating user. " + ex.Message
                Return False
            End Try
    
            Return True
        End Function
    This worked perfectly on our dev server. However, when I copied it to our web server it didn't. It took me hours of playing before I finally got it to work. I had to change the first 2 lines to

    Code:
    Dim path As String = "LDAP://_butlernt1.bc3campus/dc=bc3campus"
    Dim domainAndUsername As String = username + "@bc3campus"
    My best guess is the web server isn't able to lookup domain info. I suppose hard coding the domain name isn't a big issue since that isn't likely to change. However, I would prefer not to hard code the dc since we have 4 domain controllers. I don't what authentication to fail if the dc I hard coded happens to go down. Also, we plan on replacing at least a couple of the domain controllers soon. I'd have to change this code when we do that.

    Does anyone have any ideas on what I can do to get this to work without specifying the dc or domain name?


    Thanks,
    Scott
    Last edited by Scott MacMaster; February 27th, 2008 at 11:43 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured