国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

企業信息化系統基礎——AD:使用C#批量創建帳號

2024-07-21 02:25:28
字體:
來源:轉載
供稿:網友
,歡迎訪問網頁設計愛好者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");
}


}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 甘谷县| 绥德县| 沈丘县| 凭祥市| 科尔| 慈溪市| 舒城县| 缙云县| 五寨县| 吴江市| 九台市| 措勤县| 乐平市| 丰台区| 斗六市| 聊城市| 柏乡县| 临沧市| 巴里| 东丽区| 博湖县| 桦南县| 西昌市| 陇川县| 固原市| 晋城| 乌审旗| 忻州市| 开鲁县| 彰化市| 石家庄市| 长乐市| 克山县| 仙居县| 临猗县| 若尔盖县| 南宫市| 九龙坡区| 永新县| 延长县| 长宁区|