How to programmatically change Active Directory password

后端 未结 6 1512
执念已碎
执念已碎 2020-11-27 14:28

I have a set of test accounts that are going to be created but the accounts will be setup to require password change on the first login. I want to write a program in C# to

6条回答
  •  感动是毒
    2020-11-27 14:58

    You can use the UserPrincipal class' SetPassword method, provided you have enough privileges, once you've found the correct UserPrincipal object. Use FindByIdentity to look up the principal object in question.

    using (var context = new PrincipalContext( ContextType.Domain ))
    {
      using (var user = UserPrincipal.FindByIdentity( context, IdentityType.SamAccountName, userName ))
      {
          user.SetPassword( "newpassword" );
          // or
          user.ChangePassword( "oldPassword", "newpassword" );
    
          user.Save();
      }
    }
    

提交回复
热议问题