我們希望使用「代理用戶」連接到LDAP服務器(Active Directory,Novell或其他),然後確保嘗試登錄到應用程序的用戶鍵入了在一個可接受的用戶名和密碼。我已經獲得了用於連接到LDAP的代碼,但我不知道如何檢查用戶名和密碼。你可以通過LDAP查詢來做到這一點嗎?使用代理用戶從.NET進行LDAP身份驗證
這裏是我的代碼的膽量至今:
Public Function Authenticate(ByVal UserName As String, ByVal Password As String)
Dim LDAPServer As String = ConfigurationManager.AppSettings("LDAPServer")
Dim proxyUsername As String = ConfigurationManager.AppSettings("LDAPProxyUser")
Dim proxyPassword As String = ConfigurationManager.AppSettings("LDAPProxyPassword")
Dim entry As DirectoryEntry
entry = New DirectoryEntry(LDAPServer, proxyUsername, proxyPassword)
'This performs the LDAP authentication'
Dim obj As Object = entry.NativeObject
Dim search As New DirectorySearcher(entry)
search.Filter = String.Format("(SAMAccountName={0})", UserName)
'How do I check the password now?'
Dim result As SearchResult = search.FindOne()
If result Is Nothing Then Throw New Exception("Unable to find SAMAccountName")