“嗨,大家好,请问我在解决asp.net核心身份时遇到的问题。我已经成功地实现了通过输入到表格中的数据并传递给控制器,新用户(IdentityResult类型的对象)不会添加到数据库中。我正在使用DBConnect。以下方法(均在单独的类中)负责用户的创建(正如我提到的那样,可以正常工作)我可以在使用“自动”窗口进行调试时看到正确填充的变量),并且可以使用静态用户为数据库添加种子。感谢任何关于查找位置的建议。在此先感谢”
[HttpPost]
public async Task<IActionResult> Register(RegistrationViewModel registration)
{
if (ModelState.IsValid)
{
var user = await _userManager.FindByEmailAsync(registration.Username);
if (user == null)
{
user = new UserStore
{
Id = Guid.NewGuid().ToString(),
UserName = registration.Username
};
var result = await _userManager.CreateAsync(user, registration.Password);
}
return View();
}
return View();
}
public async Task SeedAsync()
{
_ctx.Database.EnsureCreated();
UserStore user = await _userManager.FindByEmailAsync("f.b@hotmail.com");
if(user == null)
{
user = new UserStore()
{
FirstName = "F",
SecondName = "B",
Email = "f.b@hotmail.com",
UserName = "f.b@hotmail.com"
};
var result = await _userManager.CreateAsync(user, "Password@1!");
if (result != IdentityResult.Success)
{
throw new InvalidOperationException("could not create user");
}
}