Skip to content

Commit

Permalink
Cleanup code behind
Browse files Browse the repository at this point in the history
  • Loading branch information
StuFrankish committed Jan 23, 2025
1 parent b869bbd commit 75e376c
Show file tree
Hide file tree
Showing 26 changed files with 1,027 additions and 1,286 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace IdentityServer.Pages.Account
namespace IdentityServer.Pages.Account;

public class AccessDeniedModel : PageModel
{
public class AccessDeniedModel : PageModel
public void OnGet()
{
public void OnGet()
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ namespace IdentityServer.Pages.Account.Mfa;
[AllowAnonymous]
public class Index(
SignInManager<ApplicationUser> signInManager,
UserManager<ApplicationUser> userManager,
ILogger<Index> logger) : PageModel
{
private readonly SignInManager<ApplicationUser> _signInManager = signInManager;
private readonly UserManager<ApplicationUser> _userManager = userManager;
private readonly ILogger<Index> _logger = logger;

[BindProperty]
Expand All @@ -40,12 +38,8 @@ public class InputModel
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)
{
_ = await _signInManager.GetTwoFactorAuthenticationUserAsync() ??
throw new InvalidOperationException($"Unable to load two-factor authentication user.");
}

ReturnUrl = returnUrl;
RememberMe = rememberMe;
Expand All @@ -60,20 +54,15 @@ public async Task<IActionResult> OnPostAsync(bool rememberMe, string returnUrl =
return Page();
}

returnUrl = returnUrl ?? Url.Content("~/");
returnUrl ??= Url.Content("~/");

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

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

var result = await _signInManager.TwoFactorAuthenticatorSignInAsync(authenticatorCode, rememberMe, Input.RememberMachine);

var userId = await _userManager.GetUserIdAsync(user);

if (result.Succeeded)
{
_logger.LogInformation("User with ID '{UserId}' logged in with 2fa.", user.Id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,27 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace IdentityServer.Pages.Logout
namespace IdentityServer.Pages.Logout;

[SecurityHeaders]
[AllowAnonymous]
public class LoggedOut(IIdentityServerInteractionService interactionService) : PageModel
{
[SecurityHeaders]
[AllowAnonymous]
public class LoggedOut : PageModel
{
private readonly IIdentityServerInteractionService _interactionService;
private readonly IIdentityServerInteractionService _interactionService = interactionService;

public LoggedOutViewModel View { get; set; }
public LoggedOutViewModel View { get; set; }

public LoggedOut(IIdentityServerInteractionService interactionService)
{
_interactionService = interactionService;
}
public async Task OnGet(string logoutId)
{
// get context information (client name, post logout redirect URI and iframe for federated signout)
var logout = await _interactionService.GetLogoutContextAsync(logoutId);

public async Task OnGet(string logoutId)
View = new LoggedOutViewModel
{
// get context information (client name, post logout redirect URI and iframe for federated signout)
var logout = await _interactionService.GetLogoutContextAsync(logoutId);

View = new LoggedOutViewModel
{
AutomaticRedirectAfterSignOut = LogoutOptions.AutomaticRedirectAfterSignOut,
PostLogoutRedirectUri = logout?.PostLogoutRedirectUri,
ClientName = String.IsNullOrEmpty(logout?.ClientName) ? logout?.ClientId : logout?.ClientName,
SignOutIframeUrl = logout?.SignOutIFrameUrl
};
}
AutomaticRedirectAfterSignOut = LogoutOptions.AutomaticRedirectAfterSignOut,
PostLogoutRedirectUri = logout?.PostLogoutRedirectUri,
ClientName = String.IsNullOrEmpty(logout?.ClientName) ? logout?.ClientId : logout?.ClientName,
SignOutIframeUrl = logout?.SignOutIFrameUrl
};
}
}
Original file line number Diff line number Diff line change
@@ -1,70 +1,60 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable disable

using System;
using System.Threading.Tasks;
using IdentityServer.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;

namespace IdentityServer.Areas.Identity.Pages.Account.Manage
namespace IdentityServer.Areas.Identity.Pages.Account.Manage;

public class Disable2faModel : PageModel
{
public class Disable2faModel : PageModel
private readonly UserManager<ApplicationUser> _userManager;
private readonly ILogger<Disable2faModel> _logger;

public Disable2faModel(
UserManager<ApplicationUser> userManager,
ILogger<Disable2faModel> logger)
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly ILogger<Disable2faModel> _logger;
_userManager = userManager;
_logger = logger;
}

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

public Disable2faModel(
UserManager<ApplicationUser> userManager,
ILogger<Disable2faModel> logger)
public async Task<IActionResult> OnGet()
{
var user = await _userManager.GetUserAsync(User);
if (user == null)
{
_userManager = userManager;
_logger = logger;
return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
}

/// <summary>
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
[TempData]
public string StatusMessage { get; set; }

public async Task<IActionResult> OnGet()
if (!await _userManager.GetTwoFactorEnabledAsync(user))
{
var user = await _userManager.GetUserAsync(User);
if (user == null)
{
return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
}
throw new InvalidOperationException($"Cannot disable 2FA for user as it's not currently enabled.");
}

if (!await _userManager.GetTwoFactorEnabledAsync(user))
{
throw new InvalidOperationException($"Cannot disable 2FA for user as it's not currently enabled.");
}
return Page();
}

return Page();
public async Task<IActionResult> OnPostAsync()
{
var user = await _userManager.GetUserAsync(User);
if (user == null)
{
return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
}

public async Task<IActionResult> OnPostAsync()
var disable2faResult = await _userManager.SetTwoFactorEnabledAsync(user, false);
if (!disable2faResult.Succeeded)
{
var user = await _userManager.GetUserAsync(User);
if (user == null)
{
return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
}

var disable2faResult = await _userManager.SetTwoFactorEnabledAsync(user, false);
if (!disable2faResult.Succeeded)
{
throw new InvalidOperationException($"Unexpected error occurred disabling 2FA.");
}

_logger.LogInformation("User with ID '{UserId}' has disabled 2fa.", _userManager.GetUserId(User));
StatusMessage = "2fa has been disabled. You can reenable 2fa when you setup an authenticator app";
return RedirectToPage("./TwoFactorAuthentication");
throw new InvalidOperationException($"Unexpected error occurred disabling 2FA.");
}

_logger.LogInformation("User with ID '{UserId}' has disabled 2fa.", _userManager.GetUserId(User));
StatusMessage = "2fa has been disabled. You can reenable 2fa when you setup an authenticator app";
return RedirectToPage("./TwoFactorAuthentication");
}
}
Loading

0 comments on commit 75e376c

Please sign in to comment.