如何以编程方式更改Active Directory密码 [英] How to programmatically change Active Directory password

查看:229
本文介绍了如何以编程方式更改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.

推荐答案

您可以使用<一个href="http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.userprincipal.aspx">UserPrincipal类<一href="http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.authenticableprincipal.setpassword.aspx">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" );
  }
}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆