簡體   English   中英

ASP.Net Core MVC-身份驗證上下文是否與部分視圖不同?

[英]ASP.Net Core MVC - Auth Context different for partial views?

我有一個產生2個局部視圖的模型。

  • 一個局部視圖包含用於更改用戶名的表單
  • 另一個包含更改密碼的表格

兩種形式都可以返回到UserController的POST方法。 每個部分都可以獨立正常工作。 但是,如果我更改了用戶名,然后嘗試更改密碼,則Auth Context包含舊的用戶名,並且遇到錯誤。

邏輯有點像這樣...


變更使用者名稱

調節器
 public async Task<ActionResult> ChangeUsername(ChangeUsernameViewModel model) { string oldUsername = model.OldUsername; string newUsername = model.NewUsername; User user = await this.UserService.GetUserById(this.Authorization.UserId); if (user != null) { // Update username in DB User user = await this.UserService.ChangeUsername(user, newUsername); // Update cookie this._owinContext.Authentication.SignIn(this.Authorization.ToClaimsIdentity()); // Update ViewModel model.OldUsername = newUsername; model.NewUsername = string.Empty(); } return View(model); } 
服務
 public async Task<User> ChangeUsername(User user, string newUsername) { // Blah blah blah... Code to update user with new username // and save changes to DB which is then followed by: // Change claim in Auth Context this._authorization.RemoveValue(MyClaimType.Username); this._authorization.AddValue(MyClaimType.Username, newUsername); // At this point, I can see that the Auth Context // has been updated with the new username. return user; } 


更改密碼

調節器
 public async Task<ActionResult> ChangePassword(ChangePasswordViewModel model) { string oldPassword = model.oldPassword; string newPassword = model.newPassword; User user = await UserService.GetUserByLogin(this.Authorization.Username, oldPassword); // this is where the failure occurs, so I won't // bother writing out the rest. // this.Authorization.Username is equal to "oldUsername" // that we saw in the ChangeUsername method. } 

服務中的this._authorization是否不會返回到this.Authorization控制器? 由於某些原因,每個局部視圖的this.Authorization是否不同?

清除/重置/注銷用戶會話,使用用戶提供的或從數據庫提供的新數據重新加載會話數據信息

暫無
暫無

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

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