ASP 驗證、查詢AD域賬戶信息


'''函數功能:查詢域用戶信息
'''參數說明:strAdmin-域管理賬戶;Password-域帳戶密碼;Domain-域服務器。
'''
''' 參考資料:http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/ASP/Q_23947512.html
''' 搜索:Query LDAP For Existing User   Classic ASP 在 www.experts-exchange.com
function getADUserData(strAdmin,strPassword,Domain,userName)

If  AuthenticateUser(strAdmin,strPassword,Domain)=False Then '如果管理員認證失敗則退出此過程
Response.Write "認證失敗。"
Exit function
End If

Dim Conn, strRS, RS, strConn 
Set Conn = Server.CreateObject("ADODB.Connection") 
Set RS = Server.CreateObject("ADODB.Recordset") 
 
Conn.Provider = "ADsDSOObject" 
Conn.Properties("User ID") = strAdmin
Conn.Properties("Password") = strPassword
Conn.Properties("Encrypt Password") = True
strConn = "Active Directory Provider" 
Conn.Open strConn , strAdmin, strPassword

strRS = "SELECT name FROM 'LDAP://" & Domain & "' WHERE sAMAccountName = '"&userName&"' ORDER by name " 
RS.Open strRS, Conn,1,1    

While RS.EOF = False 
getADUserData=rs.Fields("name")
RS.MoveNext 
Wend

End function 


'''函數功能:驗證域用戶賬號密碼
'''參數說明:UserName-域賬戶;Password-域帳戶密碼;Domain-域服務器。
'''返回: 用戶存在且賬號密碼正確則返回True,否則返回False;
''' 參考資料:http://stackoverflow.com/questions/3894835/ldap-asp-classic-adodb-2147217865-using-ldap-to-talk-to-active-directory
''' 搜索:LDAP + ASP Classic + ADODB  在 stackoverflow
''' 搜索:Getting Started with ASP for ADSI 在 微軟MSDN
function AuthenticateUser(UserName, Password, Domain)
dim strUser 
' assume failure 
AuthenticateUser = false  
strUser = UserName
strPassword = Password
strQuery = "SELECT cn FROM 'LDAP://" & Domain & "' WHERE objectClass='*' " 
set oConn = server.CreateObject("ADODB.Connection") 
oConn.Provider = "ADsDSOOBJECT" 
oConn.Properties("User ID") = strUser 
oConn.Properties("Password") = strPassword 
oConn.Properties("Encrypt Password") = true 
oConn.open "DS Query", strUser, strPassword
set cmd = server.CreateObject("ADODB.Command") 
set cmd.ActiveConnection = oConn 
cmd.CommandText = strQuery 
on error resume next 
set oRS = cmd.Execute 
if oRS.bof or oRS.eof then
AuthenticateUser =  false 
else    
AuthenticateUser = True 
end if 
set oRS = nothing 
set oConn = nothing  
end function


'''調用:
ADUserName=getADUserData("已知賬戶","已知密碼","yourDomain.com","要查詢的域賬戶")


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM