Set Windows/AD password so that it “never expires”?

前端 未结 3 537
没有蜡笔的小新
没有蜡笔的小新 2020-12-21 05:39

Here is my code:

using (DirectoryEntry AD = new DirectoryEntry(\"WinNT://\" + Environment.MachineName +    \",computer\"))
{
   DirectoryEntry NewUser = AD.C         


        
3条回答
  •  再見小時候
    2020-12-21 06:36

    This is my code to resolve this issue:

    // Add new user to OU
    var username = "testuser_01";
    var userDn = "LDAP://yourdomain.local:389/OU=testou,cn=yourdomain,cn=local";
    var ouUserEntry = new DirectoryEntry(userDn, "yourAdminUser", "yourAdminPassword", AuthenticationTypes.Secure);
    var newUserEntry = ouUserEntry.Children.Add("CN="+ username, "user");
    newUserEntry.Properties["sAMAccountName"].Value = username;
    newUserEntry.Properties["userPrincipalName"].Value = username + "@abc.com";
    newUserEntry.Properties["displayName"].Value = username;
    
    // Commit before enable account
    newUserEntry.CommitChanges();
    
    // Set password
    newUserEntry.Invoke("SetPassword", "yourUserPassword");
    
    // Enable Account & Password never expired (NORMAL_ACCOUNT | DONT_EXPIRE_PASSWORD)
    newUserEntry.Properties["userAccountControl"].Value = 66080; // integer value in image above
    newUserEntry.CommitChanges();
    

提交回复
热议问题