我的自定义身份用户没有更新,但它也没有给出任何错误。我要粘贴整个控制器代码,因为我认为这很重要。因为我用的是AspNetCore。身份,我不能用asp.net。身份。当我试图在另一个控制器中执行Edit操作时,我使用AspNet。标识并写入如下内容:
ApplicationUser appUser = new ApplicationUser();
appUser = UserManager.FindByEmail(email);
UserManager给出错误https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0305?f1url=%3FappId%3Droslyn%26k%3Dk(CS0305)
我发现值得一提的是,我手动设置数据库中的FirstName和姓氏,提交按钮后,它已更改为NULL。
会计总监代码
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;
简单地交换位置,它现在工作了。