Skip to content

Commit

Permalink
Refactor duplicate razor pages for reusability
Browse files Browse the repository at this point in the history
Currently mongo and sql based plugsin need to define their own razor pages which are duplicates. Refactored them in to a common library for reuse in both plugsin.
  • Loading branch information
Nfactor26 committed Mar 6, 2022
1 parent 1e94c82 commit 043bc92
Show file tree
Hide file tree
Showing 50 changed files with 148 additions and 671 deletions.
4 changes: 2 additions & 2 deletions Pixel.Identity.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pixel.Identity.Core", "src\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pixel.Identity.Messenger.Email", "src\Pixel.Identity.Messenger.Email\Pixel.Identity.Messenger.Email.csproj", "{E7F60792-6D88-40BB-A4F5-F70CC6B81396}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pixel.Identity.Store.PostgreSQL", "src\Pixel.Identity.Store.PostgreSQL\Pixel.Identity.Store.PostgreSQL.csproj", "{81343846-EC79-486E-930D-613E1B92E313}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pixel.Identity.Store.PostgreSQL", "src\Pixel.Identity.Store.PostgreSQL\Pixel.Identity.Store.PostgreSQL.csproj", "{81343846-EC79-486E-930D-613E1B92E313}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pixel.Identity.Store.Sql.Shared", "src\Pixel.Identity.Store.Sql.Shared\Pixel.Identity.Store.Sql.Shared.csproj", "{F2BB8C99-30D7-4F6E-ABEC-3F218DF60870}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pixel.Identity.Store.Sql.Shared", "src\Pixel.Identity.Store.Sql.Shared\Pixel.Identity.Store.Sql.Shared.csproj", "{F2BB8C99-30D7-4F6E-ABEC-3F218DF60870}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pixel.Identity.Store.SqlServer", "src\Pixel.Identity.Store.SqlServer\Pixel.Identity.Store.SqlServer.csproj", "{E67F20E6-B228-467A-9C40-1B38EAD35367}"
EndProject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.WebUtilities;
using System.Text;

namespace Pixel.Identity.Core.Pages
namespace Pixel.Identity.Core.Areas.Identity.Pages.Account
{
[AllowAnonymous]
[AllowAnonymous]
[IdentityDefaultUI(typeof(ConfirmEmailModel<>))]
public abstract class ConfirmEmailModel : PageModel
{
{
[TempData]
public string StatusMessage { get; set; }

public virtual Task<IActionResult> OnGetAsync(string userId, string code) => throw new NotImplementedException();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
Expand All @@ -8,38 +8,39 @@
using System.Text;
using System.Text.Encodings.Web;

namespace Pixel.Identity.Core.Pages
namespace Pixel.Identity.Core.Areas.Identity.Pages.Account
{
[AllowAnonymous]
[AllowAnonymous]
[IdentityDefaultUI(typeof(ExternalLoginModel<>))]
public class ExternalLoginModel : PageModel
{
{
[BindProperty]
public InputModel Input { get; set; }

public string ProviderDisplayName { get; set; }

public string ReturnUrl { get; set; }

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

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

public virtual IActionResult OnGet() => throw new NotImplementedException();

public virtual IActionResult OnPost(string provider, string returnUrl = null) => throw new NotImplementedException();

public virtual Task<IActionResult> OnGetCallbackAsync(string returnUrl = null, string remoteError = null) => throw new NotImplementedException();

public virtual Task<IActionResult> OnPostConfirmationAsync(string returnUrl = null) => throw new NotImplementedException();
}

public class ExternalLoginModel<TUser> : ExternalLoginModel where TUser :IdentityUser<Guid>, new()
public class ExternalLoginModel<TUser> : ExternalLoginModel where TUser : IdentityUser<Guid>, new()
{
private readonly SignInManager<TUser> signInManager;
private readonly UserManager<TUser> userManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.ComponentModel.DataAnnotations;

namespace Pixel.Identity.Core.Pages
namespace Pixel.Identity.Core.Areas.Identity.Pages.Account
{
[AllowAnonymous]
[AllowAnonymous]
[IdentityDefaultUI(typeof(LoginModel<>))]
public abstract class LoginModel : PageModel
{
{
[BindProperty]
public InputModel Input { get; set; }

public IList<AuthenticationScheme> ExternalLogins { get; set; }

public string ReturnUrl { get; set; }

[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 virtual Task OnGetAsync(string returnUrl = null) => throw new NotImplementedException();

public virtual Task<IActionResult> OnPostAsync(string returnUrl = null) => throw new NotImplementedException();
}

Expand Down Expand Up @@ -80,15 +81,15 @@ public override async Task<IActionResult> OnPostAsync(string returnUrl = null)
// To enable password failures to trigger account lockout, set lockoutOnFailure: true
var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);
if (result.Succeeded)
{
{
return LocalRedirect(returnUrl);
}
if (result.RequiresTwoFactor)
{
return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe });
}
if (result.IsLockedOut)
{
{
return RedirectToPage("./Lockout");
}
else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.ComponentModel.DataAnnotations;

namespace Pixel.Identity.Core.Pages
namespace Pixel.Identity.Core.Areas.Identity.Pages.Account
{
[AllowAnonymous]
[AllowAnonymous]
[IdentityDefaultUI(typeof(LoginWith2faModel<>))]
public abstract class LoginWith2faModel : PageModel
{
{
[BindProperty]
public InputModel Input { get; set; }

public bool RememberMe { get; set; }

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 virtual Task<IActionResult> OnGetAsync(bool rememberMe, string returnUrl = null) => throw new NotImplementedException();

public virtual Task<IActionResult> OnPostAsync(bool rememberMe, string returnUrl = null) => throw new NotImplementedException();
}

public class LoginWith2faModel<TUser> : LoginWith2faModel where TUser : IdentityUser<Guid>, new()
{
private readonly SignInManager<TUser> signInManager;
private readonly UserManager<TUser> userManager;
private readonly UserManager<TUser> userManager;

/// <summary>
/// constructor
Expand All @@ -47,7 +48,7 @@ public class InputModel
public LoginWith2faModel(SignInManager<TUser> signInManager, UserManager<TUser> userManager)
{
this.signInManager = signInManager;
this.userManager = userManager;
this.userManager = userManager;
}

public override async Task<IActionResult> OnGetAsync(bool rememberMe, string returnUrl = null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.ComponentModel.DataAnnotations;

namespace Pixel.Identity.Core.Pages
{
[AllowAnonymous]
namespace Pixel.Identity.Core.Areas.Identity.Pages.Account
{
[AllowAnonymous]
[IdentityDefaultUI(typeof(LoginWithRecoveryCodeModel<>))]
public abstract class LoginWithRecoveryCodeModel : PageModel
{
{
[BindProperty]
public InputModel Input { get; set; }

public string ReturnUrl { get; set; }

public class InputModel
{
[BindProperty]
Expand All @@ -22,22 +23,22 @@ public class InputModel
[Display(Name = "Recovery Code")]
public string RecoveryCode { get; set; }
}

public virtual Task<IActionResult> OnGetAsync(string returnUrl = null) => throw new NotImplementedException();

public virtual Task<IActionResult> OnPostAsync(string returnUrl = null) => throw new NotImplementedException();
}

public class LoginWithRecoveryCodeModel<TUser> : LoginWithRecoveryCodeModel where TUser : IdentityUser<Guid>, new()
{
private readonly SignInManager<TUser> signInManager;
private readonly UserManager<TUser> userManager;

public LoginWithRecoveryCodeModel(SignInManager<TUser> signInManager,
UserManager<TUser> userManager)
{
this.signInManager = signInManager;
this.userManager = userManager;
this.userManager = userManager;
}

public override async Task<IActionResult> OnGetAsync(string returnUrl = null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -8,37 +8,39 @@
using System.Text;
using System.Text.Encodings.Web;

namespace Pixel.Identity.Core.Pages
namespace Pixel.Identity.Core.Areas.Identity.Pages.Account
{
[AllowAnonymous]
public abstract class RegisterModel : PageModel {

[AllowAnonymous]
[IdentityDefaultUI(typeof(RegisterModel<>))]
public abstract class RegisterModel : PageModel
{

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

public string ReturnUrl { get; set; }

public IList<AuthenticationScheme> ExternalLogins { get; set; }

public class InputModel
{
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }

[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} 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; }
}

public virtual Task OnGetAsync(string returnUrl = null) => throw new NotImplementedException();

public virtual Task<IActionResult> OnPostAsync(string returnUrl = null) => throw new NotImplementedException();
Expand Down Expand Up @@ -86,7 +88,7 @@ public override async Task<IActionResult> OnPostAsync(string returnUrl = null)
var result = await _userManager.CreateAsync(user, Input.Password);

if (result.Succeeded)
{
{
var userId = await _userManager.GetUserIdAsync(user);
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Pixel.Identity.Core.Pages
namespace Pixel.Identity.Core.Areas.Identity.Pages.Account
{
[AllowAnonymous]
[IdentityDefaultUI(typeof(RegisterConfirmationModel<>))]
public class RegisterConfirmationModel : PageModel
{
public string Email { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.WebUtilities;
using System.ComponentModel.DataAnnotations;
using System.Text;

namespace Pixel.Identity.Core.Pages
namespace Pixel.Identity.Core.Areas.Identity.Pages.Account
{
[AllowAnonymous]
public abstract class ResetPasswordModel : PageModel
Expand Down
13 changes: 13 additions & 0 deletions src/Pixel.Identity.Core/Attributes/IdentityDefaultUIAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Pixel.Identity.Core
{
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class IdentityDefaultUIAttribute : Attribute
{
public IdentityDefaultUIAttribute(Type implementationTemplate)
{
Template = implementationTemplate;
}

public Type Template { get; }
}
}
Loading

0 comments on commit 043bc92

Please sign in to comment.