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"); 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) private IActionResult RedirectToLocal(string returnUrl)
{ {
if (Url.IsLocalUrl(returnUrl)) if (Url.IsLocalUrl(returnUrl))
@@ -80,5 +106,13 @@ namespace Microsoft.eShopWeb.Controllers
return RedirectToAction(nameof(CatalogController.Index), "Catalog"); return RedirectToAction(nameof(CatalogController.Index), "Catalog");
} }
} }
private void AddErrors(IdentityResult result)
{
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error.Description);
}
}
} }
} }

View File

@@ -16,5 +16,4 @@ namespace Microsoft.eShopWeb.ViewModels
[Display(Name = "Remember me?")] [Display(Name = "Remember me?")]
public bool RememberMe { get; set; } public bool RememberMe { get; set; }
} }
} }

View File

@@ -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; }
}
}

View File

@@ -0,0 +1,55 @@
@using System.Collections.Generic
@using Microsoft.AspNetCore.Http
@using Microsoft.AspNetCore.Http.Authentication
@model RegisterViewModel
@{
ViewData["Title"] = "Register";
}
<div class="brand-header-block">
<ul class="container">
<li class="active" style="margin-right: 65px;">Already have an account? <a asp-action="Signin">LOGIN</a></li>
</ul>
</div>
<div class="container account-login-container">
<div class="row">
<div class="col-md-12">
<section>
<form asp-controller="Account" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" class="form-horizontal">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="Email" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="Password" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="ConfirmPassword" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="ConfirmPassword" class="form-control" />
<span asp-validation-for="ConfirmPassword" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default btn-brand btn-brand-big">&nbsp;REGISTER&nbsp;</button>
</div>
<p>
Note that for demo purposes you don't need to register! Use the credentials shown below the
<a asp-action="signin">login screen</a>.
</p>
</form>
</section>
</div>
</div>
</div>
@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}

View File

@@ -35,7 +35,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Views\Catalog\" /> <Folder Include="Views\Catalog\" />
<Folder Include="Views\Account\" />
<Folder Include="wwwroot\css\catalog\" /> <Folder Include="wwwroot\css\catalog\" />
<Folder Include="wwwroot\fonts\" /> <Folder Include="wwwroot\fonts\" />
</ItemGroup> </ItemGroup>