Recently, a test environment is required for training customers. Because the system uses domain user verification, it takes a lot of effort to establish a large number of Domain Users, in addition, the user name in the test environment can be set up according to the user organization and role, for example, role. unit
Therefore, using C # To create users in batches, we found two difficult problems:
1. How to set the initial password of the user name
2. How to Set "Change Password Upon next login" not selected (selected by default)
The final test establishes the user's Code As follows: 1 Directoryentry ad = New Directoryentry ( " LDAP: // dc = cnblogs, Dc = com " , " Administrator " , " PA $ word " , Authenticationtypes. Secure );
2 Directorysearcher searcher = New Directorysearcher (AD );
3 Searcher. Filter = String. Format ( " Ou = {0} " , " Blogs " );
4 Searchresult result = Searcher. findone ();
5 If (Result ! = Null )
6 {
7 String Samaccountname = " Tester " ;
8 String Displayname = " Test Creation " ;
9 Directoryentry userentry = Result. getdirectoryentry (). Children. Add (string. Format ( " CN = {0} " , Samaccountname ), " User " );
10 Userentry. properties [ " Samaccountname " ]. Add (samaccountname );
11 Userentry. properties [ " Displayname " ]. Add (displayname );
12 Userentry. properties [ " Userpassword " ]. Add ( " PA $ word " );
13 Userentry. properties [ " Useraccountcontrol " ]. Value = 544 ;
14 Userentry. properties [ " Pwdlastset " ]. Value = - 1 ;
15 Userentry. commitchanges ();
16 Userentry. Invoke ( " Setpassword " , New Object [] { " PA $ word " });
17 }
Note:
1. userentry. Invoke ("setpassword", new object [] {"pa $ word"}); must be called after userentry. commitchanges;
2. userentry. properties ["pwdlastset"]. value =-1; Set "the user must change the password upon next login" to be deselected. Refer to the link below.
Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/ADSI/modifying_user_cannot_change_password_ldap_provider.asp
By The Way, make a small recruitment advertisement. Haha, sorry.
A friend recently asked me to recommend several Program staff in Beijing. net programmers can send their resumes to me at
bestcomy at hotmail.com (replace at with @)
Please write your contact information on your resume, if appropriate, someone will contact you.