簡體   English   中英

LDAP 更改/重置密碼 c# mvc

[英]LDAP change/reset password c# mvc

我試圖更改/重置密碼給在第一次使用 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 訪問用戶?(我成功使用正確的用戶登錄)

我創建了可以管理所有用戶的管理員用戶,然后我讓所有用戶使用管理員並找到想要的用戶並為此用戶設置密碼:

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"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM