Implementing Registration. (#32)

This commit is contained in:
Steve Smith
2017-08-07 15:21:03 -04:00
committed by GitHub
parent 1193af57ef
commit a8f150aac7
5 changed files with 112 additions and 2 deletions

View File

@@ -69,6 +69,32 @@ namespace Microsoft.eShopWeb.Controllers
return RedirectToAction(nameof(CatalogController.Index), "Catalog");
}
[AllowAnonymous]
public IActionResult Register()
{
return View();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await _signInManager.SignInAsync(user, isPersistent: false);
return RedirectToLocal(returnUrl);
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
private IActionResult RedirectToLocal(string returnUrl)
{
if (Url.IsLocalUrl(returnUrl))
@@ -80,5 +106,13 @@ namespace Microsoft.eShopWeb.Controllers
return RedirectToAction(nameof(CatalogController.Index), "Catalog");
}
}
private void AddErrors(IdentityResult result)
{
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error.Description);
}
}
}
}