【发布时间】:2021-08-04 22:23:39
【问题描述】:
我的自定义身份用户没有更新,但也没有给出任何错误。 我正在粘贴整个控制器代码,因为我认为它很重要。因为我使用的是 AspNetCore.Identity,所以不能使用 AspNet.Identity。当我尝试在另一个控制器中执行编辑操作时,我使用 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)
我发现值得一提的是,我在数据库中手动设置了 FirstName 和 Surname,并且在提交按钮后它已更改为 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");
}
}
}
【问题讨论】:
-
请说明你得到的编译错误。
-
_userManager.FindByEmailAsync(email);返回什么?什么对象? -
@Adleri ApplicationUser
标签: c# asp.net-mvc asp.net-core asp.net-identity