LDAP change/reset 密码 c# mvc

LDAP change/reset password c# mvc

我尝试 change/reset 密码给在使用 c#

首次登录后必须更改密码的用户

我的代码:

var domain = WebConfigurationManager.AppSettings["ONLINE-AD"];
directoryEntry.Username = userName;
directoryEntry.Password = password;
var directorySearcher = new DirectorySearcher(directoryEntry);
SearchResult result = directorySearcher.FindOne();
if (result != null)
  {
    DirectoryEntry userEntry = result.GetDirectoryEntry();
    if (userEntry != null)
    {
      userEntry.Invoke("SetPassword", model.Resetpassword);
      userEntry.CommitChanges();
     }
   }

但是当我尝试 FindOne() 时出现错误 773(意味着用户必须第一次更改密码)

这是错误:

如何使用 LDAP 访问用户?(我成功登录了 正确的用户)

我创建了可以管理所有用户的 Admin 用户,然后我让所有用户使用管理员并找到想要的用户并为此用户设置密码:

public string ResetPassword(LoginDTO model) {
  try {
    //get context by admin user

    PrincipalContext ctx = new PrincipalContext(ContextType.Domain, WebConfigurationManager.AppSettings["ONLINE-AD"], WebConfigurationManager.AppSettings["AdminName"], WebConfigurationManager.AppSettings["AdminPassword"]);

    //find the wanted user
    var user = UserPrincipal.FindByIdentity(ctx, model.UserName);

    if (user != null) {
      try {
        user.ChangePassword(model.Password, model.NewPassword);
      } catch {
        return "-1";
      }

    }

  } catch (Exception ex) {
    return "-1";
  }
  return "1"
}