最近在練習登入及註冊功能
我是參考這部影片
https://youtu.be/R5Zw3fn2YE4?t=1977
用的是內建的Identity機制
我有在Table AspNetUsers 裡追加兩個欄位
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
最終畫面是這樣
這是相關的Controller
[HttpPost]
public async Task<IActionResult> Create(ApplicationUser user)
{
if (ModelState.IsValid)
{
var result = await _userManager.CreateAsync(user, user.PasswordHash);
if (result.Succeeded)
{
var isSaveRole = await _userManager.AddToRoleAsync(user,"User");
TempData["save"] = "User has been created successfully";
return RedirectToAction(nameof(Index));
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty,error.Description);
}
}
return View();
}
只是每次一跑到isSaveRole這邊時就會出錯,最後就呈現標題那樣的錯誤
想請問我是錯在哪裡呢?
這是我的GIT
https://github.com/Jarkwoof/ShoppingCart
路徑是Customer/User/Create
拜託了,謝謝