views:

785

answers:

4

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 go through the test accounts and change the passwords.

+2  A: 

Here's a great Active Directory programming quick reference:

Howto: (Almost) Everything In Active Directory via C#

See the password reset code near the end.

Dana Holt
+2  A: 

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" );
    }
}
tvanfosson
That's only available in .NET 3.5 and up, BTW (the PrincipalContext and all).
marc_s
A: 

Hi ,

Thanks a lotfor ur solution.. it worked like a charm for me.

nandini
A: 

here is the solution in code :

string newPassword = Membership.GeneratePassword(12, 4); string quotePwd; byte[] pwdBin;

quotePwd = String.Format(@"""{0}""", newPassword);

pwdBin = System.Text.Encoding.Unicode.GetBytes(quotePwd);

UserEntry.Properties["unicodePwd"].Value = pwdBin;

UserEntry.CommitChanges();

;) Mohamed Hachem

Mohamed Hachem