If a company intends to use Microsoft's products to build your own office automation system, it is recommended to use the main domain control. Then, you must use the Activity Directory (AD), so that IT departments need to create domain accounts for every employee of the company. This is a big project if the company is bigger. Moreover, we will find that some workloads are basically repetitive, and the Human Resources Department will enroll a variety of information, such as the name, labor number, department, department leader, telephone number, department leader, telephone number Wait, then, IT staff after got this table, he has to re-entry once. And often cause an error in the account because of human causes. Below, we have written a program that created an account with C #. In this program, it will not only create domain accounts, which also creates the corresponding mail account in the corresponding Exchange. Through this procedure, the human resource department only needs to fill in the relevant items in the database format (Access) provided by the IT department.
First, we need to define some variables:
String strmemberof = ""
String struserparm = "";
String strmanager = ""
String strscriptpath = ""
String strDepartment = "";
String strcompany = "";
// String straccountexp;
String defaultnc = "DC = TEST, DC = Net"; // This is the default domain
String alias = "";
String fullname = ""
String password = @ "password"; // This is the default initial password
String DomainName = "Test.net";
String strgivenname = "";
// The following variable tells the procedure to build mailbox in which storage area of Exchange
String homemdb = "cn = test, cn = holding company,"
"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 = "Start loading data from the template!";
// Get template information
We know that there are many projects that have been created, so we first create an account as a template, then by reading the data of this template as the corresponding item of the new account.
This code uses AD's query object:
DirectoryEntry DEMB = New DirectoryEntry ();
DEMB.PATH = "
LDAP: // CN = Template, OU = Project Group, OU = Department, 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 ();
Stroperties = DEMB.PROPERTIES ["Company"] [0] .tostring ();
// straccountexp = DEMB.PROPERTIES ["AccountExpires"]. Value.toString ();
DEMB.CLOSE ();
Label1.text = "Load data is completed! Start reading new account information from the database!";
// Read the database to get account information
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 '= 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
{
// Start creating an account
//Messagebox.show (objrs.fields [2] .value.toString ());
DirectoryEntry Container, User;
CDOEXM.ImailboxStore Mailbox;
Container = New DirectoryEntry ("
LDAP: // OU = Project Group, Ou = Department, " Defaultnc);
// read data
While (! Objrs.eof)
{
// read data
Fullname = objrs.fields [1] .value.toString ();
Alias = Objrs.fields [4] .value.toString ();
Strgivenname = objrs.fields [2] .value.toString ();
Label1.text = "Create an account:" Fullname "-" Alias "-" Strgivenname "checks if there is no repetition account!"
/ / Check if there is a repetitive account
DirectoryEntry Su = New DirectoryEntry ("
LDAP: // DC = TEST, DC = Net ");
DirectorySearcher Searcher = new DirectorySearcher ();
Searcher.searchRoot = SU;
Searcher.filter = "(" samaccountname = " alias ")) ";
Searcher.searchscope = searchscope.subtree;
Searcher.sort = New Sortoption ("Givenname", sortdirection.ascending;
SearchResultCollection results = Searcher.FindAll ();
IF (Results.count> 0)
{
// Indicates that there are repeated accounts, modify Fullname and Alias
Fullname = Fullname strgivenname;
Alias = alias strgivenname;
}
// else
// {
// Create an account
Label1.text = "Create an account:" Fullname "-" Alias " Strgivenname;
Try
{
User = container.children.add ("cn =" fullname, "user");
User.properties ["samaccountname"]. add (alias); // account
User.properties ["UserPrincipalName"]. Add ((Alias "@ Test.Net")); user.properties ["givenname"]. add (strgivename); // labor number
User.properties ["sn"]. add (fullname); // surname
// User.properties ["Telephonenumber"]. Add ("0000"); // Phone
// User.properties ["Mobile"]. Add ("00000000000"); // Mobile phone
User.properties ["Company"]. Add (strcompany); // Company
User.properties ["department"]. add (strDepartment); // department
// User.properties ["physicalDeliveryofficename"]. Add ("0000");
/ / Here you will explain: Here is the expiration time to set the account, because, according to our provisions, if the account is not passed before the account is expired, the account will be disabled. However, this field in the AD is shaped, I don't know how to converse it, so there is the following code, hope, the master can give a point.
DateTime DT = New DateTime (2004, 10, 31, 0, 0, 0);
Long longae = dt.ticks;
Longae = longae-504910656000000000; // minus 8 time zones
User.properties ["accountexpires"]. add (longae.tostring ()); // account expiration time
User.properties ["msnpallowdialin"]. Value = false; // Prohibition of dialing
User.properties ["UserParameters"]. add (struser); // Disable terminal service
User.properties ["scriptpath"]. add (strscriptpath); // configuration file
User.properties ["manager"]. add (strmanager); // leadership
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 = "Create an account:" Fullname "-" Alias "-" Strgivenname "created!";
Objrs.movenext ();
}
}
Catch (SystemException EX)
{
Objconn.close ();
Messagebox.show (ex.Message);
}
Finally
{
Objrs.close ();
Objconn.close ();
Messagebox.show ("OK");
}
}