How to Set the "password must be changed upon next login" attribute (LDAP provider) of the AD user)

Source: Internet
Author: User
Tags samaccountname
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.

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.