Skip to content

Commit

Permalink
Cleanup v max
Browse files Browse the repository at this point in the history
  • Loading branch information
victorrar committed Dec 23, 2020
1 parent 85c87f7 commit 8df8904
Show file tree
Hide file tree
Showing 56 changed files with 443 additions and 578 deletions.
111 changes: 62 additions & 49 deletions .idea/.idea.TRPZ_PrintService/.idea/workspace.xml

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions TRPZ_PrintService/Areas/Identity/IdentityHostingStartup.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using TRPZ_PrintService.Areas.Identity;
using TRPZ_PrintService.Areas.Identity.Data;
using TRPZ_PrintService.Data;

[assembly: HostingStartup(typeof(TRPZ_PrintService.Areas.Identity.IdentityHostingStartup))]
[assembly: HostingStartup(typeof(IdentityHostingStartup))]

namespace TRPZ_PrintService.Areas.Identity
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace TRPZ_PrintService.Areas.Identity.Pages.Account
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
Expand All @@ -15,8 +12,8 @@ namespace TRPZ_PrintService.Areas.Identity.Pages.Account
[AllowAnonymous]
public class ConfirmEmailChangeModel : PageModel
{
private readonly UserManager<TRPZ_PrintServiceUser> _userManager;
private readonly SignInManager<TRPZ_PrintServiceUser> _signInManager;
private readonly UserManager<TRPZ_PrintServiceUser> _userManager;

public ConfirmEmailChangeModel(UserManager<TRPZ_PrintServiceUser> userManager,
SignInManager<TRPZ_PrintServiceUser> signInManager)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.Security.Claims;
using System.Text;
using System.Text.Encodings.Web;
Expand All @@ -20,10 +17,10 @@ namespace TRPZ_PrintService.Areas.Identity.Pages.Account
[AllowAnonymous]
public class ExternalLoginModel : PageModel
{
private readonly SignInManager<TRPZ_PrintServiceUser> _signInManager;
private readonly UserManager<TRPZ_PrintServiceUser> _userManager;
private readonly IEmailSender _emailSender;
private readonly ILogger<ExternalLoginModel> _logger;
private readonly SignInManager<TRPZ_PrintServiceUser> _signInManager;
private readonly UserManager<TRPZ_PrintServiceUser> _userManager;

public ExternalLoginModel(
SignInManager<TRPZ_PrintServiceUser> signInManager,
Expand All @@ -45,11 +42,6 @@ public ExternalLoginModel(

[TempData] public string ErrorMessage { get; set; }

public class InputModel
{
[Required] [EmailAddress] public string Email { get; set; }
}

public IActionResult OnGetAsync()
{
return RedirectToPage("./Login");
Expand Down Expand Up @@ -93,18 +85,16 @@ public async Task<IActionResult> OnGetCallbackAsync(string returnUrl = null, str
{
return RedirectToPage("./Lockout");
}
else
{
// If the user does not have an account, then ask the user to create an account.
ReturnUrl = returnUrl;
ProviderDisplayName = info.ProviderDisplayName;
if (info.Principal.HasClaim(c => c.Type == ClaimTypes.Email))
Input = new InputModel
{
Email = info.Principal.FindFirstValue(ClaimTypes.Email)
};
return Page();
}

// If the user does not have an account, then ask the user to create an account.
ReturnUrl = returnUrl;
ProviderDisplayName = info.ProviderDisplayName;
if (info.Principal.HasClaim(c => c.Type == ClaimTypes.Email))
Input = new InputModel
{
Email = info.Principal.FindFirstValue(ClaimTypes.Email)
};
return Page();
}

public async Task<IActionResult> OnPostConfirmationAsync(string returnUrl = null)
Expand Down Expand Up @@ -136,15 +126,15 @@ public async Task<IActionResult> OnPostConfirmationAsync(string returnUrl = null
var callbackUrl = Url.Page(
"/Account/ConfirmEmail",
null,
new {area = "Identity", userId = userId, code = code},
new {area = "Identity", userId, code},
Request.Scheme);

await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

// If account confirmation is required, we need to show the link if we don't have a real email sender
if (_userManager.Options.SignIn.RequireConfirmedAccount)
return RedirectToPage("./RegisterConfirmation", new {Email = Input.Email});
return RedirectToPage("./RegisterConfirmation", new {Input.Email});

await _signInManager.SignInAsync(user, false, info.LoginProvider);

Expand All @@ -159,5 +149,10 @@ await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
ReturnUrl = returnUrl;
return Page();
}

public class InputModel
{
[Required] [EmailAddress] public string Email { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text.Encodings.Web;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
Expand All @@ -17,8 +15,8 @@ namespace TRPZ_PrintService.Areas.Identity.Pages.Account
[AllowAnonymous]
public class ForgotPasswordModel : PageModel
{
private readonly UserManager<TRPZ_PrintServiceUser> _userManager;
private readonly IEmailSender _emailSender;
private readonly UserManager<TRPZ_PrintServiceUser> _userManager;

public ForgotPasswordModel(UserManager<TRPZ_PrintServiceUser> userManager, IEmailSender emailSender)
{
Expand All @@ -28,11 +26,6 @@ public ForgotPasswordModel(UserManager<TRPZ_PrintServiceUser> userManager, IEmai

[BindProperty] public InputModel Input { get; set; }

public class InputModel
{
[Required] [EmailAddress] public string Email { get; set; }
}

public async Task<IActionResult> OnPostAsync()
{
if (ModelState.IsValid)
Expand Down Expand Up @@ -62,5 +55,10 @@ await _emailSender.SendEmailAsync(

return Page();
}

public class InputModel
{
[Required] [EmailAddress] public string Email { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace TRPZ_PrintService.Areas.Identity.Pages.Account
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace TRPZ_PrintService.Areas.Identity.Pages.Account
Expand Down
43 changes: 19 additions & 24 deletions TRPZ_PrintService/Areas/Identity/Pages/Account/Login.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
Expand All @@ -18,9 +15,9 @@ namespace TRPZ_PrintService.Areas.Identity.Pages.Account
[AllowAnonymous]
public class LoginModel : PageModel
{
private readonly UserManager<TRPZ_PrintServiceUser> _userManager;
private readonly SignInManager<TRPZ_PrintServiceUser> _signInManager;
private readonly ILogger<LoginModel> _logger;
private readonly SignInManager<TRPZ_PrintServiceUser> _signInManager;
private readonly UserManager<TRPZ_PrintServiceUser> _userManager;

public LoginModel(SignInManager<TRPZ_PrintServiceUser> signInManager,
ILogger<LoginModel> logger,
Expand All @@ -39,17 +36,6 @@ public LoginModel(SignInManager<TRPZ_PrintServiceUser> signInManager,

[TempData] public string ErrorMessage { get; set; }

public class InputModel
{
[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 OnGetAsync(string returnUrl = null)
{
if (!string.IsNullOrEmpty(ErrorMessage)) ModelState.AddModelError(string.Empty, ErrorMessage);
Expand Down Expand Up @@ -83,21 +69,30 @@ public async Task<IActionResult> OnPostAsync(string returnUrl = null)
}

if (result.RequiresTwoFactor)
return RedirectToPage("./LoginWith2fa", new {ReturnUrl = returnUrl, RememberMe = Input.RememberMe});
return RedirectToPage("./LoginWith2fa", new {ReturnUrl = returnUrl, Input.RememberMe});
if (result.IsLockedOut)
{
_logger.LogWarning("User account locked out.");
return RedirectToPage("./Lockout");
}
else
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return Page();
}

ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return Page();
}

// If we got this far, something failed, redisplay form
return Page();
}

public class InputModel
{
[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; }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
Expand All @@ -15,8 +13,8 @@ namespace TRPZ_PrintService.Areas.Identity.Pages.Account
[AllowAnonymous]
public class LoginWith2faModel : PageModel
{
private readonly SignInManager<TRPZ_PrintServiceUser> _signInManager;
private readonly ILogger<LoginWith2faModel> _logger;
private readonly SignInManager<TRPZ_PrintServiceUser> _signInManager;

public LoginWith2faModel(SignInManager<TRPZ_PrintServiceUser> signInManager, ILogger<LoginWith2faModel> logger)
{
Expand All @@ -30,25 +28,12 @@ public LoginWith2faModel(SignInManager<TRPZ_PrintServiceUser> signInManager, ILo

public string ReturnUrl { get; set; }

public class InputModel
{
[Required]
[StringLength(7, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.",
MinimumLength = 6)]
[DataType(DataType.Text)]
[Display(Name = "Authenticator code")]
public string TwoFactorCode { get; set; }

[Display(Name = "Remember this machine")]
public bool RememberMachine { get; set; }
}

public async Task<IActionResult> OnGetAsync(bool rememberMe, string returnUrl = null)
{
// Ensure the user has gone through the username & password screen first
var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();

if (user == null) throw new InvalidOperationException($"Unable to load two-factor authentication user.");
if (user == null) throw new InvalidOperationException("Unable to load two-factor authentication user.");

ReturnUrl = returnUrl;
RememberMe = rememberMe;
Expand All @@ -63,7 +48,7 @@ public async Task<IActionResult> OnPostAsync(bool rememberMe, string returnUrl =
returnUrl = returnUrl ?? Url.Content("~/");

var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();
if (user == null) throw new InvalidOperationException($"Unable to load two-factor authentication user.");
if (user == null) throw new InvalidOperationException("Unable to load two-factor authentication user.");

var authenticatorCode = Input.TwoFactorCode.Replace(" ", string.Empty).Replace("-", string.Empty);

Expand All @@ -76,17 +61,29 @@ await _signInManager.TwoFactorAuthenticatorSignInAsync(authenticatorCode, rememb
_logger.LogInformation("User with ID '{UserId}' logged in with 2fa.", user.Id);
return LocalRedirect(returnUrl);
}
else if (result.IsLockedOut)

if (result.IsLockedOut)
{
_logger.LogWarning("User with ID '{UserId}' account locked out.", user.Id);
return RedirectToPage("./Lockout");
}
else
{
_logger.LogWarning("Invalid authenticator code entered for user with ID '{UserId}'.", user.Id);
ModelState.AddModelError(string.Empty, "Invalid authenticator code.");
return Page();
}

_logger.LogWarning("Invalid authenticator code entered for user with ID '{UserId}'.", user.Id);
ModelState.AddModelError(string.Empty, "Invalid authenticator code.");
return Page();
}

public class InputModel
{
[Required]
[StringLength(7, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.",
MinimumLength = 6)]
[DataType(DataType.Text)]
[Display(Name = "Authenticator code")]
public string TwoFactorCode { get; set; }

[Display(Name = "Remember this machine")]
public bool RememberMachine { get; set; }
}
}
}
Loading

0 comments on commit 8df8904

Please sign in to comment.