From a8f150aac77b62655385317c66b993fe104926b9 Mon Sep 17 00:00:00 2001 From: Steve Smith Date: Mon, 7 Aug 2017 15:21:03 -0400 Subject: [PATCH] Implementing Registration. (#32) --- src/Web/Controllers/AccountController.cs | 34 +++++++++++++++ src/Web/ViewModels/LoginViewModel.cs | 1 - src/Web/ViewModels/RegisterViewModel.cs | 23 ++++++++++ src/Web/Views/Account/Register.cshtml | 55 ++++++++++++++++++++++++ src/Web/Web.csproj | 1 - 5 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 src/Web/ViewModels/RegisterViewModel.cs create mode 100644 src/Web/Views/Account/Register.cshtml diff --git a/src/Web/Controllers/AccountController.cs b/src/Web/Controllers/AccountController.cs index ccaffa8..abcb55d 100644 --- a/src/Web/Controllers/AccountController.cs +++ b/src/Web/Controllers/AccountController.cs @@ -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 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); + } + } } } diff --git a/src/Web/ViewModels/LoginViewModel.cs b/src/Web/ViewModels/LoginViewModel.cs index e5e09be..8a86677 100644 --- a/src/Web/ViewModels/LoginViewModel.cs +++ b/src/Web/ViewModels/LoginViewModel.cs @@ -16,5 +16,4 @@ namespace Microsoft.eShopWeb.ViewModels [Display(Name = "Remember me?")] public bool RememberMe { get; set; } } - } diff --git a/src/Web/ViewModels/RegisterViewModel.cs b/src/Web/ViewModels/RegisterViewModel.cs new file mode 100644 index 0000000..1724461 --- /dev/null +++ b/src/Web/ViewModels/RegisterViewModel.cs @@ -0,0 +1,23 @@ +using System.ComponentModel.DataAnnotations; + +namespace Microsoft.eShopWeb.ViewModels +{ + public class RegisterViewModel + { + [Required] + [EmailAddress] + [Display(Name = "Email")] + public string Email { get; set; } + + [Required] + [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] + [DataType(DataType.Password)] + [Display(Name = "Password")] + public string Password { get; set; } + + [DataType(DataType.Password)] + [Display(Name = "Confirm password")] + [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] + public string ConfirmPassword { get; set; } + } +} diff --git a/src/Web/Views/Account/Register.cshtml b/src/Web/Views/Account/Register.cshtml new file mode 100644 index 0000000..85a0668 --- /dev/null +++ b/src/Web/Views/Account/Register.cshtml @@ -0,0 +1,55 @@ +@using System.Collections.Generic +@using Microsoft.AspNetCore.Http +@using Microsoft.AspNetCore.Http.Authentication +@model RegisterViewModel +@{ + ViewData["Title"] = "Register"; +} +
+
    +
  • Already have an account? LOGIN
  • +
+
+ + + @section Scripts { + @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } + } diff --git a/src/Web/Web.csproj b/src/Web/Web.csproj index 82708dd..937c90b 100644 --- a/src/Web/Web.csproj +++ b/src/Web/Web.csproj @@ -35,7 +35,6 @@ -