如何以编程方式更改Active Directory密码密码、方式、Active、Directory

2023-09-08 12:04:35 作者:我很想说出口

我有一组测试帐户将要创建,但帐户将被设置为需要在首次登录时更改密码。我想用C#程序要经过测试帐户并更改密码。

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.

推荐答案

您可以使用UserPrincipal类SetPassword方法,只要你有足够的权限,一旦你找到了正确的UserPrincipal对象。使用 FindByIdentity 的主要对象查找问题。

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" );
  }
}