asp访问ad目录通过ldap

转载自:http://forums.aspfree.com/code-bank-54/asp-code-query-ad-ldap-169652.html

the following code let's you query a domain AD to get users details.

I've split the query into two parts, so if you want to get more than one users details and want different info for each user you can add another sub and get what info you want.

<%@ LANGUAGE=VBSCRIPT %>
<%Option Explicit%>
<%
Function getADUserInfo(strUID)
	on error resume next
	strGeneralLookupError = false
	strBase = "<LDAP://DC=[DOMAIN], DC=[DOMAIN EXETENTION]>"
	strFilter = "(sAMAccountName=" & strUID & ")" 
	strAttributes = "cn, mail, company, givenName, sn, ADsPath, name, sAMAccountName, telephoneNumber"
	'strAttributes = "cn, company, givenName, sn, ADsPath, name, sAMAccountName, telephoneNumber"
	strScope = "subtree"	
	strFullCommand = strBase & ";" & strFilter & ";" & strAttributes & ";" & strScope
	set rsADUserInfo = Server.CreateObject("ADODB.Recordset")
	set	rsADUserInfo = connAD.Execute(strFullCommand)
	if err.number <> 0 then
		strGeneralLookupError = true
	end if
	set getADUserInfo = rsADUserInfo
	set rsADUserInfo = Nothing
End Function
Sub getUserData(p_strUserID)
	on error resume next
	set rsUserData = Server.CreateObject("ADODB.Recordset")
	set rsUserData = getADUserInfo(p_strUserID)
	if not rsUserData.EOF then
		strUserGN = rsUserData("givenName")
		strUserSN = rsUserData("sn")
		strUserOU = rsUserData("company")
		strUserEmail = rsUserData("mail")
		strUserPhone = rsUserData("telephoneNumber")
	else
		strADLookupSuccess = false
	end if
	rsUserData.Close
	set rsUserData = Nothing
End Sub
on error resume next
response.expires = 0
DIM connAD, rsUserData, rsADUserInfo
DIM strUserGN, strUserSN, strUserOU, strUserEmail, strUserPhone
DIM strBase, strFilter,strAttributes, strScope, strFullCommand
DIM strGeneralLookupError, strADLookupSuccess
DIM strUserID
strUserGN = "The user can not be found in the system."
strGeneralLookupError = false
strADLookupSuccess = true
set connAD = Server.CreateObject("ADODB.Connection")
connAD.Provider = "ADsDSOObject"
connAD.Properties("User ID") = "[DOMAIN]\[USERNAME]" ' ### remember to make sure this user has rights to access AD
connAD.Properties("Password") = "[PASSWORD]"
connAD.Properties("Encrypt Password") = true
connAD.Open
strUserID = "[USERNAME YOU WANT INFO FOR]"
call getUserData(strUserID)
connAD.Close
set connAD = Nothing
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>ASP Code to access AD with LDAP Page</title>
</head>
<body>
<%=strUserGN%>&nbsp;
<%=strUserSN%><br />
<%=strUserOU%><br />
<%=strUserEmail%><br />
<%=strUserPhone%><br />
</body>
</html>

 

加支付宝好友偷能量挖...


评论(0)网络
阅读(204)喜欢(0)Asp/VBScript