Por que os métodos Register e Login do AccountController do ASP.NET Identity estão causando erro de rota?
01/09/2018
0
Ocorre o seguinte erro:
"InvalidOperationException: The model item passed into the ViewDataDictionary is of type ''Novateca.Web.Models.AccountViewModels.RegisterViewModel'', but this ViewDataDictionary instance requires a model item of type ''Novateca.Web.Models.ApplicationUser''."
Seguem abaixo o trechos de código dos métodos em questão:
Register:
Login:
"InvalidOperationException: The model item passed into the ViewDataDictionary is of type ''Novateca.Web.Models.AccountViewModels.RegisterViewModel'', but this ViewDataDictionary instance requires a model item of type ''Novateca.Web.Models.ApplicationUser''."
Seguem abaixo o trechos de código dos métodos em questão:
Register:
[HttpGet] [AllowAnonymous] public IActionResult Register(string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; return View(); } [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var user = new ApplicationUser { FirstName = model.FirstName, LastName = model.LastName, UserName = model.Email, Email = model.Email, Profile = "user"}; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); // var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); // var callbackUrl = Url.Action(nameof(ConfirmEmail), "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme); // await IEmailSender.SendEmailAsync(model.Email, "Confirm your account", $"Por favor, confirme seu email clicando neste link: <a href=''''>link</a>"); await _signInManager.SignInAsync(user, isPersistent: false); _logger.LogInformation("User created a new account with password."); return RedirectToLocal(returnUrl); } AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); }
Login:
[HttpGet] [AllowAnonymous] public async Task<IActionResult> Login(string returnUrl = null) { // Clear the existing external cookie to ensure a clean login process await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme); ViewData["ReturnUrl"] = returnUrl; return View(); } [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { // This doesn''t count login failures towards account lockout // To enable password failures to trigger account lockout, set lockoutOnFailure: true var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false); if (result.Succeeded) { _logger.LogInformation("User logged in."); return RedirectToLocal(returnUrl); } if (result.IsLockedOut) { _logger.LogWarning("User account locked out."); return RedirectToAction(nameof(Lockout)); } else { ModelState.AddModelError("InvalidLogin", "O usuário ou senha incorreto."); return View(model); } } // If we got this far, something failed, redisplay form return View(model); }
Cesar Murilo
Curtir tópico
+ 0
Responder
Clique aqui para fazer login e interagir na Comunidade :)