,欢迎访问网页设计爱好者web开发。如果一个公司打算使用微软的产品来构建自己的办公自动化系统,那么,建议采用主域控制的方式。那么,必然就要用到活动目录(ad),这样,it部门就需要为公司的每一个员工来创建域帐号。如果公司比较大的话,这是一个很大的工程。而且,我们会发现,有些工作量基本上是在重复劳动,人力资源部为了给it部门提供人员名单,会录入一次人员的各种信息,比如姓名、工号、所属部门、部门领导、电话号码等等,那么,it人员在拿到这张表后,他又要重新录入一次。并且常常会因为人为的原因导致帐户中出现错误。下面,我们就用c#编写了一个创建帐户的程序。在这个程序中,它不但要创建域帐户,它还会在相应的exchange中创建相应的邮件帐户。通过这个程序,人力资源部门只需要按照it部门提供的数据库格式(access)填写相关项目就可以了。
首先,我们需要定义一些变量:
string strmemberof="";
 string struserparm="";
 string strmanager="";
 string strscriptpath="";
 string strdepartment="";
 string strcompany="";
 // string straccountexp;
 string defaultnc = "dc=test,dc=net"; //这是默认的域
 string alias = ""; 
 string fullname = "";
 string password = @"password"; //这是默认的初始密码
 string domainname = "test.net";
 string strgivenname="";
 
//下面这个变量告诉程序将邮箱建在exchange的哪个存储区域中
string homemdb = "cn=test,cn=控股公司," 
 + "cn=informationstore,cn=mail,cn=servers,"
 + "cn=first administrative group,cn=administrative groups,"
 + "cn=test,cn=microsoft exchange,cn=services,"
 + "cn=configuration,dc=test,dc=net";
 
label1.text="开始从模板中加载数据!";
 //获取模板信息
 
我们知道,创建的一批帐户中,有许多的项目是相同的,所以,我们先创建好一个帐户作为模板,然后,通过读取这个模板的数据作为新建的帐户的相应项目的数据。
这段代码采用了ad的查询对象:
 directoryentry demb = new directoryentry();
 demb.path="ldap://cn=模板, ou=项目组,ou=部门,dc=test, dc=net";
 strmemberof=demb.properties["memberof"][0].tostring();
 struserparm=demb.properties["userparameters"][0].tostring();
 strmanager=demb.properties["manager"][0].tostring();
 strscriptpath=demb.properties["scriptpath"][0].tostring();
 strdepartment=demb.properties["department"][0].tostring();
 strcompany=demb.properties["company"][0].tostring();
 // straccountexp=demb.properties["accountexpires"].value.tostring();
 demb.close();
 label1.text="加载数据完毕!开始从数据库中读取新建帐户信息!";
 //读取数据库获取帐户信息
 adodb.connection objconn;
 adodb.command objcmd;
 adodb.recordset objrs;
 object objoptparm;
 objoptparm="";
 string [email protected]"jet oledb:global partial bulk ops=2;jet oledb:registry path=;jet oledb:database locking mode=1;data source=""db1.mdb"";mode=share deny none;jet oledb:engine type=5;provider=""microsoft.jet.oledb.4.0"";jet oledb:system database=;jet oledb:sfp=false;persist security info=false;extended properties=;jet oledb:compact without replica repair=false;jet oledb:encrypt database=false;jet oledb:create system database=false;jet oledb:don't copy locale on compact=false;user id=admin;jet oledb:global bulk transactions=1";
 objconn=new adodb.connection();
 try
 {
 objconn.open(str,"","",-1);
 
 }
 catch(systemexception ex)
 {
 messagebox.show(ex.message);
 
 }
 finally
 {
 //
 }
 objrs=new adodb.recordset();
 objcmd=new adodb.command();
 objcmd.commandtext="select * from sheet1";
 objcmd.activeconnection=objconn;
 try
 {
 objrs=objcmd.execute(out objoptparm,ref objoptparm,1);
 }
 catch(systemexception ex)
 {
 objconn.close();
 messagebox.show(ex.message);
 
 
 }
 finally
 {
 //
 }
 try
 {
 
 //开始创建帐户 
 //messagebox.show(objrs.fields[2].value.tostring());
 directoryentry container, user;
 cdoexm.imailboxstore mailbox;
 container = new directoryentry("ldap://ou=项目组,ou=部门," + defaultnc);
 //读取数据
 while (!objrs.eof)
 {
 //读取数据
 fullname=objrs.fields[1].value.tostring();
 alias=objrs.fields[4].value.tostring();
 strgivenname=objrs.fields[2].value.tostring();
 label1.text="创建帐户:"+fullname+"-"+alias+"-"+strgivenname+"检查有无重复帐号!";
 //检查是否有重复的帐号
 directoryentry su=new directoryentry("ldap://dc=test,dc=net");
 directorysearcher searcher = new directorysearcher();
 searcher.searchroot=su;
 searcher.filter = "(&(objectclass=user)(samaccountname="+alias+"))";
 searcher.searchscope = searchscope.subtree;
 searcher.sort = new sortoption("givenname", sortdirection.ascending);
 searchresultcollection results = searcher.findall();
 if(results.count>0)
 {
 //表明有重复的帐号,修改fullname和alias
 fullname=fullname+strgivenname;
 alias=alias+strgivenname;
 
 }
 // else
 // {
 //创建帐户
 label1.text="创建帐户:"+fullname+"-"+alias+"-"+strgivenname;
 try
 {
 
 user = container.children.add("cn=" + fullname, "user");
 user.properties["samaccountname"].add(alias);//帐户
 user.properties["userprincipalname"].add((alias+"@test.net"));
 user.properties["givenname"].add(strgivenname);//工号
 user.properties["sn"].add(fullname);//姓
 // user.properties["telephonenumber"].add("0000");//电话
 // user.properties["mobile"].add("00000000000");//手机
 user.properties["company"].add(strcompany);//公司
 user.properties["department"].add(strdepartment);//部门
 // user.properties["physicaldeliveryofficename"].add("0000");
 
//这里要说明一下:这里是要设置帐户的到期时间,因为,根据我们的规定,如果在帐户到期之前,没有通过考试的话,那么帐户就会禁用。可是,ad中这个字段是整形的,我不知道怎么去换算它,所以就有以下这段代码,希望,有高手可以指点一下。
 datetime dt=new datetime(2004,10,31,0,0,0,0);
 long longae=dt.ticks;
 longae=longae-504910656000000000;//减去8个时区
 user.properties["accountexpires"].add(longae.tostring());//帐号到期时间
 
 user.properties["msnpallowdialin"].value=false;//禁止拨入
 user.properties["userparameters"].add(struserparm);//禁止终端服务
 user.properties["scriptpath"].add(strscriptpath);//配置文件
 user.properties["manager"].add(strmanager);//领导
 user.properties["userpassword"].add(password);
 
 // user.invoke("setpassword", new object[]{password});
 user.commitchanges();
 user.invoke("setpassword", new object[]{password});
 user.commitchanges();
 //this enables the new user.
 user.properties["useraccountcontrol"].value = 0x200; //ads_uf_normal_account
 user.commitchanges();
 
 //obtain the imailboxstore interface, create the mailbox, and commit the changes.
 mailbox = (imailboxstore)user.nativeobject;
 mailbox.createmailbox(homemdb);
 user.commitchanges();
 
 
 }
 catch(exception ex) 
 {
 messagebox.show(ex.message.tostring());
 }
 
 // }
 
 label1.text="创建帐户:"+fullname+"-"+alias+"-"+strgivenname+"创建完毕!";
 objrs.movenext();
 
 }
 
 }
 catch(systemexception ex)
 { 
 objconn.close();
 messagebox.show(ex.message);
 }
 finally
 {
 objrs.close();
 objconn.close();
 messagebox.show("ok");
 }
 
 
 }