公司資訊化系統基礎——AD:使用C#大量建立帳號

來源:互聯網
上載者:User
建立 如果一個公司打算使用微軟的產品來構建自己的辦公自動化系統,那麼,建議採用主域控制的方式。那麼,必然就要用到活動目錄(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 str=@"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");
}


}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.