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"; +} +
+ +
+
+
+
+
+
+
+
+ +
+ + +
+
+
+ +
+ + +
+
+
+ +
+ + +
+
+
+ +
+

+ Note that for demo purposes you don't need to register! Use the credentials shown below the + login screen. +

+
+
+
+
+
+ + @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 @@ -