小编典典

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

c#

我有一组要创建的测试帐户,但是将设置这些帐户以在首次登录时要求更改密码。我想用C#编写程序以通过测试帐户并更改密码。


阅读 313

收藏
2020-05-19

共1个答案

小编典典

找到正确的UserPrincipal对象后,可以使用UserPrincipal类的SetPassword方法,前提是您具有足够的特权。使用FindByIdentity查找有问题的主体对象。

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();
  }
}
2020-05-19