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

How to programmatically change Active Directory password(如何以编程方式更改 Active Directory 密码)

本文介绍了如何以编程方式更改 Active Directory 密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组将要创建的测试帐户,但这些帐户将被设置为在首次登录时要求更改密码.我想用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" );

      user.Save();
  }
}

这篇关于如何以编程方式更改 Active Directory 密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何以编程方式更改 Active Directory 密码

基础教程推荐