前段时间写出一个程序,只是不完善,不能判断是否存在该用户,现在补充查找功能。
代码不足之处,希不吝指教!
完整代码如下:
<%@ language=vbscript codepage="936"%>
<% option explicit %>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<meta name="generator" content="microsoft visual studio 7.0">
</head>
<body>
<%
程序目的:创建exchange 2000 邮箱用户
程序实现方式:首先在ad(active directory:活动目录)中查找是否有该用
户,如果有则提示用户该名字已经存在,没有则创建该用户,
并为该用户创建邮箱。
程序设计:skyword, skyword@21cn.com
程序日期:2001-06-27
需要注意的问题:程序使用中,应当关注ldap的不同。并且程序的运行需要比
较主高的运行权限(我使用是管理员权限,呵呵比较不安全,
大家在实际使用中要注意,不然被人窃取了帐号不要怪我。
我也不太会设置),需要在iis中设置(以前就是因为不了解
,走了不少弯路:) )
另外还有一种方案,查找用户可结合数据库查找,我想大家
会更熟悉一些,只是要记得在初始数据时,要把计算机内所
有的帐号都要记录进数据库,不然就不能准备判断是否该用
户,因为ad只判断是否有该用户,而不管该在什么位置,这
点大家要注意。
程序是中用到一些知识点均加以注释,希望对大家对所帮助
另外需要注意的是帐号名字不能小于2位,而且不能使用中文
名字.
dim objuser
dim objcontainer
dim objmailbox
dim objmail
dim recipname, recip
dim servername, domainname, emailname, firstname, lastname, password
判断用户是否存在
dim strquery, objconn, objrs, strresult
查询语句:语法:要找什么,即查找基(<ldap://dc=program,dc=org>);
在目录的什么地方找(&(objectcategory=person)(samaccountname=用户
名))(用户为?的用户;找到对象的属性(samaccount,adspath)(用户名,
ldap地址);查找的范围(subtree:查找搜索基以下的整个子树中)。例句
就是下面这句.
具体细节请参看《asp3高级编程》p644,机械工业出版社(好象精华区内
有这本书的电子文档,这本本书真的不错,值得购买)
strquery = "<ldap://dc=program,dc=org>;(&(objectcategory=person)" & _
"(samaccountname=" & request.form ("txtusername") & "));" & _
"samaccountname,adspath;subtree"
打开adsi
set objconn = server.createobject ("adodb.connection")
objconn.provider = "adsdsoobject"
下面这句是用合法用户打开,不然查找的结果有误,本例中使用管理员帐号
objconn.open "active directory provider","cn=administrator, cn=users, dc=program, dc=org","skyword"
查找用户是否存在,不存在则创建用户
set objrs = server.createobject ("adodb.recordset")
objrs.open strquery, objconn
if not objrs.eof then
strresult = false
response.write "用户已经存在"
else
创建用户
strresult = true
end if
objrs.close
set objrs = nothing
objconn.close
set objconn = nothing
if strresult = true then
创建用户的代码
servername,domainname是adsi需要的ldap信息,是必需的。
servername = "skyword.program.org"
domainname = "dc=program,dc=org"
emailname = request.form ("txtusername")
password = request.form ("txtpassword")
recip = "cn=" & emailname
下面这语个if语句没多少价值。 :)
if request.form ("txttruename") <> "" then
lastname = "sky"
firstname = "word"
else
lastname = "last"
firstname = "name"
end if
打开对象
set objcontainer = getobject("ldap://" & servername & "/ou=china.org," & domainname)
创建帐号
set objuser = objcontainer.create("user", recip)
objuser.put "samaccountname", emailname 帐号
objuser.put "sn", lastname
objuser.put "givenname", firstname
objuser.put "displayname", emailname & "@hina.org" 显示的名字
objuser.put "mail", emailname & "@china.org"
objuser.put "userprincipalname", emailname & "@china.org" 用户登录帐号
objuser.setinfo 写进ad中
objuser.setpassword password 用户密码
objuser.accountdisabled = false 帐号生效
objuser.setinfo 写进ad,一定要,为什么我也不清楚
创建邮箱
set objmailbox = objuser
下面的ldap是查出来的,安装不同,ldap会有所不同。
objmailbox.createmailbox "ldap://skyword.program.org/" & _
"cn=mailbox store (skyword),cn=first storage group," & _
"cn=informationstore,cn=skyword,cn=servers," & _
"cn=first administrative group,cn=administrative groups," & _
"cn=ecitye,cn=microsoft exchange,cn=services," & _
"cn=configuration,dc=program,dc=org"
objuser.setinfo
set objcontainer = nothing
set objuser = nothing
set objmailbox = nothing
下面是简单的错误处理。
if err <> 0 then
response.write "创建用户失败!请重试一次<br>"
response.write "<input type=button value=重试一次 onclick=vbscript:history.back()> "
response.write "<input type=button value=返回 onclick=vbscript:window.location.href=index.html>"
else
以下是简单的测试
set objmail = server.createobject ("cdonts.newmail")
objmail.send "admins@china.org", emailname & "@china.org", "welcome", "welcome use china.org mail"
set objmail = nothing
response.write emailname & "用户已经成功创建!!!!" & "<br>三秒后导向登录页!"
response.addheader "refresh","3;url=http://mail.china.org"
end if
end if
%>
</body>
</html>
