< Summary

Class:MUNityCore.Areas.Identity.Pages.Account.LoginModel
Assembly:MUNity.BlazorServer
File(s):C:\Users\aeuke\source\repos\PeerConradi\munity\src\MUNity.BlazorServer\Areas\Identity\Pages\Account\Login.cshtml.cs
Covered lines:0
Uncovered lines:52
Coverable lines:52
Total lines:122
Line coverage:0% (0 of 52)
Covered branches:0
Total branches:14
Branch coverage:0% (0 of 14)
Covered methods:0
Total methods:12
Method coverage:0% (0 of 12)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%10%
get_Input()100%10%
get_LoginOutput()100%10%
get_ExternalLogins()100%10%
get_ReturnUrl()100%10%
get_ErrorMessage()100%10%
get_Username()100%10%
get_Password()100%10%
get_RememberMe()100%10%
get_InvalidInput()100%10%
OnGetAsync()0%40%
OnPostAsync()0%100%

File(s)

C:\Users\aeuke\source\repos\PeerConradi\munity\src\MUNity.BlazorServer\Areas\Identity\Pages\Account\Login.cshtml.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel.DataAnnotations;
 4using System.Linq;
 5using System.Text.Encodings.Web;
 6using System.Threading.Tasks;
 7using Microsoft.AspNetCore.Authorization;
 8using Microsoft.AspNetCore.Authentication;
 9using Microsoft.AspNetCore.Identity;
 10using Microsoft.AspNetCore.Identity.UI.Services;
 11using Microsoft.AspNetCore.Mvc;
 12using Microsoft.AspNetCore.Mvc.RazorPages;
 13using Microsoft.Extensions.Logging;
 14using MUNity.Database.Models.User;
 15
 16namespace MUNityCore.Areas.Identity.Pages.Account
 17{
 18    [AllowAnonymous]
 19    public class LoginModel : PageModel
 20    {
 21        private readonly UserManager<MunityUser> _userManager;
 22        private readonly SignInManager<MunityUser> _signInManager;
 23        private readonly ILogger<LoginModel> _logger;
 24
 025        public LoginModel(SignInManager<MunityUser> signInManager,
 026            ILogger<LoginModel> logger,
 027            UserManager<MunityUser> userManager)
 028        {
 029            _userManager = userManager;
 030            _signInManager = signInManager;
 031            _logger = logger;
 032        }
 33
 34        [BindProperty]
 035        public InputModel Input { get; set; }
 36
 37        [BindProperty]
 038        public OutputModel LoginOutput { get; set; }
 39
 040        public IList<AuthenticationScheme> ExternalLogins { get; set; }
 41
 042        public string ReturnUrl { get; set; }
 43
 44        [TempData]
 045        public string ErrorMessage { get; set; }
 46
 47        public class InputModel
 48        {
 49            [Required]
 050            public string Username { get; set; }
 51
 52            [Required]
 53            [DataType(DataType.Password)]
 054            public string Password { get; set; }
 55
 56            [Display(Name = "Remember me?")]
 057            public bool RememberMe { get; set; }
 58        }
 59
 60        public class OutputModel
 61        {
 062            public bool InvalidInput { get; set; }
 63        }
 64
 65        public async Task OnGetAsync(string returnUrl = null)
 066        {
 067            if (!string.IsNullOrEmpty(ErrorMessage))
 068            {
 069                ModelState.AddModelError(string.Empty, ErrorMessage);
 070            }
 71
 072            returnUrl ??= Url.Content("~/");
 73
 74            // Clear the existing external cookie to ensure a clean login process
 075            await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
 76
 077            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
 78
 079            ReturnUrl = returnUrl;
 080        }
 81
 82        public async Task<IActionResult> OnPostAsync(string returnUrl = null)
 083        {
 084            returnUrl ??= Url.Content("~/");
 85
 086            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
 87
 088            if (ModelState.IsValid)
 089            {
 90                // This doesn't count login failures towards account lockout
 91                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
 092                var result = await _signInManager.PasswordSignInAsync(Input.Username, Input.Password, Input.RememberMe, 
 093                if (result.Succeeded)
 094                {
 095                    _logger.LogInformation($"{Input.Username} logged in.");
 096                    return LocalRedirect(returnUrl);
 97                }
 098                if (result.RequiresTwoFactor)
 099                {
 0100                    return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe }
 101                }
 0102                if (result.IsLockedOut)
 0103                {
 0104                    _logger.LogWarning("User account locked out.");
 0105                    return RedirectToPage("./Logout");
 106                }
 107                else
 0108                {
 0109                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
 0110                    return Page();
 111                }
 112            }
 113            else
 0114            {
 0115                LoginOutput.InvalidInput = true;
 0116            }
 117
 118            // If we got this far, something failed, redisplay form
 0119            return Page();
 0120        }
 121    }
 122}