Basic--AD of Enterprise Information System: Create accounts in bulk using C #

Source: Internet
Author: User
Tags compact time zones ldap mail account

Basic--AD of Enterprise Information System: Create accounts in bulk using C #

If a company intends to use Microsoft's products to build its own office automation system, it is recommended to adopt the primary domain control approach. Then, it is necessary to use Active Directory, so that the IT department needs to create a domain account for every employee in the company. If the company is larger, this is a very big project. Moreover, we will find that some of the workload is basically repetitive work, the Human resources department in order to give IT department personnel list, will input a person's various information, such as name, work number, department, department leader, phone number, etc., then, IT staff in the table, he has to re-enter once again. And there are often errors in the account due to human causes. Below, we have written a program to create an account in C #. In this program, not only does it create a domain account, it also creates the appropriate mail account in the appropriate exchange. With this program, the HR department only needs to fill in the relevant project according to 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 program where to build the 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 many of the items created in the batch account are the same, so we first create the good one account as a template and then, by reading this template data as the corresponding item of the new account. This code uses the query object of the ad:
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 ();
Strcompany=demb.properties["Company"][0]. ToString ();
straccountexp=demb.properties["Accountexpires"]. Value.tostring ();
Demb.close ();
Label1. text= "Loading data is complete! Start reading new account information from the database! ";
Read database get account information
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
{

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);
Reading data while (!objrs.eof)
{
Reading data
FULLNAME=OBJRS.FIELDS[1]. Value.tostring ();
ALIAS=OBJRS.FIELDS[4]. Value.tostring ();
STRGIVENNAME=OBJRS.FIELDS[2].     Value.tostring (); Label1. text= "Create account:" +fullname+ "-" +alias+ "-" +strgivenname+ "Check for duplicate accounts! ";
Check to see if duplicate accounts are available
DirectoryEntry Su=new DirectoryEntry ("Ldap://dc=test,dc=net"); DirectorySearcher searcher = new DirectorySearcher ();
Searcher. SEARCHROOT=SU;
Searcher. Filter = "(& (object= searchscope.subtree;
Searcher. Sort = new Sortoption ("GivenName", sortdirection.ascending);
SearchResultCollection results = searcher. FindAll ();
if (results. COUNT>0)
{
Indicate duplicate accounts, modify FullName and Alias
Fullname=fullname+strgivenname;
Alias=alias+strgivenname;

}
Else
// {
Create an Account
Label1. text= "Create 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 (strgivenname);//Work No.
User. properties["SN"]. ADD (fullName);//Surname
User. properties["Telephonenumber"]. ADD ("0000");//Telephone
User. properties["mobile"]. ADD ("00000000000");//Mobile
User. Properties["Company"). ADD (Strcompany);//Company
User. properties["department"]. ADD (strdepartment);//Department
User. properties["physicalDeliveryOfficeName"]. ADD ("0000"); Here's a note: Here is the time to set the account expiration, because, according to our rules, if the exam is not passed before the account expires, then the account will be disabled. However, this field in the ad is shaped, I do not know how to convert it, so there is the following code, I hope, there are experts can point.
DateTime dt=new datetime (2004,10,31,0,0,0,0);
Long Longae=dt. Ticks;
longae=longae-504910656000000000;//minus 8 time zones
User. properties["Accountexpires"]. ADD (Longae.tostring ());//Account expiry time
User. properties["msNPAllowDialin"]. value=false;//Prohibit dial-in
User. properties["Userparameters"]. ADD (struserparm);//Disable Terminal Services
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 account:" +fullname+ "-" +alias+ "-" +strgivenname+ "created! ";
Objrs.movenext ();

}
}
catch (SystemException ex)
{
Objconn.close ();
MessageBox.Show (ex.   Message); }
Finally
{
Objrs.close ();
Objconn.close ();
MessageBox.Show ("OK");
}
}

Category:Active Directory

Basic--AD of Enterprise Information System: Create accounts in bulk using C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.