如何在不知道旧密码的情况下,使用目录服务更改Active Directory用户密码?
如何在不知道旧密码的情况下,使用目录服务更改Active Directory用户密码?
SetPassword
方法,您应该在DirectoryEntry
对象上调用它。DirectoryEntry entry = new DirectoryEntry("LDAP://CN=johndoe,CN=Users,DC=acme,DC=com");
在 .net 3.5 中引入了一个名为 System.DirectoryServices.AccountManagement 的新库,它简化了用户管理相关的工作。
public void ChangePassword(string dn, string newPassword)
{
using (var context = new PrincipalContext(ContextType.Domain))
{
using (var user = UserPrincipal.FindByIdentity(context, IdentityType.DistinguishedName, dn))
{
user.SetPassword(newPassword);
}
}
}