Adding Razor Pages Version (#60)
* In progress copying code into new RP project Cleaning up namespaces and whitespace in original Web project * Cleaning up some more namespaces * Removing unused page. * Index page loads correctly. * Fixing up paging. * Moving views; getting ready to convert to RPs * Auto stash before merge of "master" and "origin/master" Basket and Checkout pages wired up * WIP on Account pages * Working on signin/signout * Working on auth * Getting order history working Fixing auth bug * Fixing Checkout issue * Fixing link
This commit is contained in:
54
src/WebRazorPages/Pages/Account/Register.cshtml
Normal file
54
src/WebRazorPages/Pages/Account/Register.cshtml
Normal file
@@ -0,0 +1,54 @@
|
||||
@page
|
||||
@model RegisterModel
|
||||
@{
|
||||
ViewData["Title"] = "Register";
|
||||
}
|
||||
<div class="brand-header-block">
|
||||
<ul class="container">
|
||||
<li class="active" style="margin-right: 65px;">Already have an account?
|
||||
<a asp-page="/Account/Signin">LOGIN</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="container account-login-container">
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<section>
|
||||
<form 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="UserDetails.Email" class="col-md-2 control-label"></label>
|
||||
<div class="col-md-10">
|
||||
<input asp-for="UserDetails.Email" class="form-control" />
|
||||
<span asp-validation-for="UserDetails.Email" class="text-danger"></span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="UserDetails.Password" class="col-md-2 control-label"></label>
|
||||
<div class="col-md-10">
|
||||
<input asp-for="UserDetails.Password" class="form-control" />
|
||||
<span asp-validation-for="UserDetails.Password" class="text-danger"></span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="UserDetails.ConfirmPassword" class="col-md-2 control-label"></label>
|
||||
<div class="col-md-10">
|
||||
<input asp-for="UserDetails.ConfirmPassword" class="form-control" />
|
||||
<span asp-validation-for="UserDetails.ConfirmPassword" class="text-danger"></span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<button type="submit" class="btn btn-default btn-brand btn-brand-big"> REGISTER </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"); }
|
||||
}
|
||||
53
src/WebRazorPages/Pages/Account/Register.cshtml.cs
Normal file
53
src/WebRazorPages/Pages/Account/Register.cshtml.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.eShopWeb.RazorPages.ViewModels;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Infrastructure.Identity;
|
||||
|
||||
namespace Microsoft.eShopWeb.RazorPages.Pages.Account
|
||||
{
|
||||
public class RegisterModel : PageModel
|
||||
{
|
||||
private readonly SignInManager<ApplicationUser> _signInManager;
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
|
||||
public RegisterModel(SignInManager<ApplicationUser> signInManager,
|
||||
UserManager<ApplicationUser> userManager
|
||||
)
|
||||
{
|
||||
_signInManager = signInManager;
|
||||
_userManager = userManager;
|
||||
}
|
||||
|
||||
[BindProperty]
|
||||
public RegisterViewModel UserDetails { get; set; }
|
||||
|
||||
|
||||
public async Task<IActionResult> OnPost(string returnUrl = "/Index")
|
||||
{
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
var user = new ApplicationUser { UserName = UserDetails.Email, Email = UserDetails.Email };
|
||||
var result = await _userManager.CreateAsync(user, UserDetails.Password);
|
||||
if (result.Succeeded)
|
||||
{
|
||||
await _signInManager.SignInAsync(user, isPersistent: false);
|
||||
return LocalRedirect(returnUrl);
|
||||
}
|
||||
AddErrors(result);
|
||||
}
|
||||
return Page();
|
||||
|
||||
}
|
||||
|
||||
private void AddErrors(IdentityResult result)
|
||||
{
|
||||
foreach (var error in result.Errors)
|
||||
{
|
||||
ModelState.AddModelError("", error.Description);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
64
src/WebRazorPages/Pages/Account/Signin.cshtml
Normal file
64
src/WebRazorPages/Pages/Account/Signin.cshtml
Normal file
@@ -0,0 +1,64 @@
|
||||
@page
|
||||
@using System.Collections.Generic
|
||||
@using Microsoft.AspNetCore.Http
|
||||
@using Microsoft.AspNetCore.Http.Authentication
|
||||
@model SigninModel
|
||||
@{
|
||||
ViewData["Title"] = "Log in";
|
||||
}
|
||||
<div class="brand-header-block">
|
||||
<ul class="container">
|
||||
@*<li><a asp-area="" asp-controller="Account" asp-action="Register">REGISTER</a></li>*@
|
||||
<li class="active" style="margin-right: 65px;">LOGIN</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="container account-login-container">
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<section>
|
||||
<form asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" class="form-horizontal">
|
||||
<h4>ARE YOU REGISTERED?</h4>
|
||||
<div asp-validation-summary="All" class="text-danger"></div>
|
||||
<div class="form-group">
|
||||
<label asp-for="LoginDetails.Email" class="control-label form-label"></label>
|
||||
<input asp-for="LoginDetails.Email" class="form-control form-input form-input-center" />
|
||||
<span asp-validation-for="LoginDetails.Email" class="text-danger"></span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="LoginDetails.Password" class="control-label form-label"></label>
|
||||
<input asp-for="LoginDetails.Password" class="form-control form-input form-input-center" />
|
||||
<span asp-validation-for="LoginDetails.Password" class="text-danger"></span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div class="checkbox">
|
||||
<label asp-for="LoginDetails.RememberMe">
|
||||
<input asp-for="LoginDetails.RememberMe" />
|
||||
@Html.DisplayNameFor(m => m.LoginDetails.RememberMe)
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<button type="submit" class="btn btn-default btn-brand btn-brand-big"> LOG IN </button>
|
||||
</div>
|
||||
<p>
|
||||
<a asp-page="/Account/Register"
|
||||
asp-route-returnurl="@ViewData["ReturnUrl"]" class="text">Register as a new user?</a>
|
||||
</p>
|
||||
<p>
|
||||
Note that for demo purposes you don't need to register and can login with these credentials:
|
||||
</p>
|
||||
<p>
|
||||
User: <b>demouser@microsoft.com</b>
|
||||
</p>
|
||||
<p>
|
||||
Password: <b>Pass@word1</b>
|
||||
</p>
|
||||
</form>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@section Scripts {
|
||||
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
|
||||
}
|
||||
80
src/WebRazorPages/Pages/Account/Signin.cshtml.cs
Normal file
80
src/WebRazorPages/Pages/Account/Signin.cshtml.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.eShopWeb.RazorPages.ViewModels;
|
||||
using Microsoft.eShopWeb.RazorPages.Interfaces;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Infrastructure.Identity;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Microsoft.eShopWeb.RazorPages.Pages.Account
|
||||
{
|
||||
public class SigninModel : PageModel
|
||||
{
|
||||
private readonly SignInManager<ApplicationUser> _signInManager;
|
||||
private readonly IBasketService _basketService;
|
||||
|
||||
public SigninModel(SignInManager<ApplicationUser> signInManager,
|
||||
IBasketService basketService)
|
||||
{
|
||||
_signInManager = signInManager;
|
||||
_basketService = basketService;
|
||||
}
|
||||
|
||||
[BindProperty]
|
||||
public LoginViewModel LoginDetails { get; set; } = new LoginViewModel();
|
||||
|
||||
public class LoginViewModel
|
||||
{
|
||||
[Required]
|
||||
[EmailAddress]
|
||||
public string Email { get; set; }
|
||||
|
||||
[Required]
|
||||
[DataType(DataType.Password)]
|
||||
public string Password { get; set; }
|
||||
|
||||
[Display(Name = "Remember me?")]
|
||||
public bool RememberMe { get; set; }
|
||||
}
|
||||
|
||||
|
||||
public async Task OnGet(string returnUrl = null)
|
||||
{
|
||||
await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
|
||||
|
||||
ViewData["ReturnUrl"] = returnUrl;
|
||||
if (!String.IsNullOrEmpty(returnUrl) &&
|
||||
returnUrl.IndexOf("checkout", StringComparison.OrdinalIgnoreCase) >= 0)
|
||||
{
|
||||
ViewData["ReturnUrl"] = "/Basket/Index";
|
||||
}
|
||||
}
|
||||
public async Task<IActionResult> OnPost(string returnUrl = null)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return Page();
|
||||
}
|
||||
ViewData["ReturnUrl"] = returnUrl;
|
||||
|
||||
var result = await _signInManager.PasswordSignInAsync(LoginDetails.Email,
|
||||
LoginDetails.Password, LoginDetails.RememberMe, lockoutOnFailure: false);
|
||||
if (result.Succeeded)
|
||||
{
|
||||
string anonymousBasketId = Request.Cookies[Constants.BASKET_COOKIENAME];
|
||||
if (!String.IsNullOrEmpty(anonymousBasketId))
|
||||
{
|
||||
await _basketService.TransferBasketAsync(anonymousBasketId, LoginDetails.Email);
|
||||
Response.Cookies.Delete(Constants.BASKET_COOKIENAME);
|
||||
}
|
||||
return RedirectToPage(returnUrl ?? "/Index");
|
||||
}
|
||||
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
|
||||
return Page();
|
||||
}
|
||||
}
|
||||
}
|
||||
6
src/WebRazorPages/Pages/Account/Signout.cshtml
Normal file
6
src/WebRazorPages/Pages/Account/Signout.cshtml
Normal file
@@ -0,0 +1,6 @@
|
||||
@page
|
||||
@model SignoutModel
|
||||
@{
|
||||
ViewData["Title"] = "Signing out";
|
||||
}
|
||||
<h2>Signing out...</h2>
|
||||
32
src/WebRazorPages/Pages/Account/Signout.cshtml.cs
Normal file
32
src/WebRazorPages/Pages/Account/Signout.cshtml.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Infrastructure.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Microsoft.eShopWeb.RazorPages.Pages.Account
|
||||
{
|
||||
public class SignoutModel : PageModel
|
||||
{
|
||||
private readonly SignInManager<ApplicationUser> _signInManager;
|
||||
|
||||
public SignoutModel(SignInManager<ApplicationUser> signInManager)
|
||||
{
|
||||
_signInManager = signInManager;
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnGet()
|
||||
{
|
||||
await _signInManager.SignOutAsync();
|
||||
|
||||
return RedirectToPage("/Index");
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPost()
|
||||
{
|
||||
await _signInManager.SignOutAsync();
|
||||
|
||||
return RedirectToPage("/Index");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user