Cyberherbalist
Cyberherbalist

Reputation: 12309

How does one require a password to be changed when setting an Active Directory password programmatically?

I am using the following code to reset a user's Active Directory password.

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

But I need to be able to require the user to change the password after the first time they log in. I can't find a method or setting or property that does the job, however. It is evident that this can be done, I just can't find out how!

Upvotes: 2

Views: 302

Answers (1)

marc_s
marc_s

Reputation: 754250

You need to expire the newly created password right away - try this:

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

See the MSDN docs on ExpirePasswordNow for more details

Upvotes: 4

Related Questions