gpt4 book ai didi

c# - 身份用户数据未更新

转载 作者:行者123 更新时间:2023-12-04 07:18:14 25 4
gpt4 key购买 nike

我的自定义身份用户没有更新,但也没有出现任何错误。
我正在粘贴整个 Controller 代码,因为我认为这很重要。因为我使用的是 AspNetCore.Identity,所以我不能使用 AspNet.Identity。当我尝试在另一个 Controller 中执行编辑操作时,我使用 AspNet.Identity 并编写如下内容:

ApplicationUser appUser = new ApplicationUser();
appUser = UserManager.FindByEmail(email);
UserManager 给出错误 https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0305?f1url=%3FappId%3Droslyn%26k%3Dk(CS0305)
我发现值得一提的是,我在数据库中手动设置了名字和姓氏,提交按钮后它已更改为 NULL。
帐户 Controller 代码
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using MySocialService.Data;
using MySocialService.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MySocialService.Controllers
{
public class AccountController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _singInManager;

public AccountController(UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager)
{
_userManager = userManager;
_singInManager = signInManager;
}

public IActionResult Register()
{
return View();
}

[HttpPost]
public async Task<IActionResult> Register(UserRegistrationModel model)
{
if(ModelState.IsValid)
{
var user = new ApplicationUser
{
UserName = model.Email,
Email = model.Email,
FirstName = model.FirstName,
Surname = model.Surname,
Gender = model.Gender
};

var result = await _userManager.CreateAsync(user, model.Password);

if(result.Succeeded)
{
await _singInManager.SignInAsync(user, isPersistent: false);
return RedirectToAction("Index", "Home");
}

foreach(var error in result.Errors)
{
ModelState.AddModelError("", error.Description);
}

ModelState.AddModelError(string.Empty, "Invalid Login Attempt");
}

return View(model);
}

[HttpGet]
[AllowAnonymous]
public IActionResult Login()
{
return View();
}
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> Login(UserLoginModel user)
{
if (ModelState.IsValid)
{
var result = await _singInManager.PasswordSignInAsync(user.Email, user.Password,user.RememberMe,false);

if (result.Succeeded)
{
return RedirectToAction("Dashboard", "MainPage");
}

ModelState.AddModelError(string.Empty, "Invalid Login Attempt");

}
return View(user);
}

public async Task<IActionResult> Logout()
{
await _singInManager.SignOutAsync();

return RedirectToAction("Index", "Home");
}

[HttpGet]
public async Task<IActionResult> Edit(string email)
{
ApplicationUser appUser = new ApplicationUser();
appUser = await _userManager.FindByEmailAsync(email);
UserRegistrationModel model = new UserRegistrationModel();
model.FirstName = appUser.FirstName;
model.Surname = appUser.Surname;
return View(model);
}

[HttpPost]
public async Task<IActionResult> Edit(UserRegistrationModel model)
{
var currentUser = await _userManager.FindByEmailAsync(model.Email);
model.FirstName = currentUser.FirstName;
model.Surname = currentUser.Surname;

await _userManager.UpdateAsync(currentUser);

return RedirectToAction("Dashboard", "MainPage");
}
}
}

最佳答案

我通过调试找到了答案。在评论部分@Adleri 询问什么对象 _userManager.FindByEmailAsync(email)返回所以我检查了它。它返回 ApplicationUser 所以我改变了 var currentUser = await _userManager.FindByEmailAsync(model.Email);

ApplicationUser currentUser = new ApplicationUser();
currentUser = await _userManager.FindByEmailAsync(model.Email);
然后我发现 currentUser 字段为空所以我改变了
 model.FirstName = currentUser.FirstName;
model.Surname = currentUser.Surname;
currentUser.FirstName = model.FirstName;
currentUser.Surname = model.Surname;
只需交换位置,它现在就可以工作。

关于c# - 身份用户数据未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68658614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com