|
-
February 25th, 2008, 09:11 PM
#1
[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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|