Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Linq;
  5. using System.Text.Encodings.Web;
  6. using System.Threading.Tasks;
  7. using Microsoft.AspNetCore.Authorization;
  8. using CinemaWorld.Data.Models;
  9. using Microsoft.AspNetCore.Authentication;
  10. using Microsoft.AspNetCore.Identity;
  11. using Microsoft.AspNetCore.Identity.UI.Services;
  12. using Microsoft.AspNetCore.Mvc;
  13. using Microsoft.AspNetCore.Mvc.RazorPages;
  14. using Microsoft.Extensions.Logging;
  15.  
  16. namespace CinemaWorld.Web.Areas.Identity.Pages.Account
  17. {
  18.     [AllowAnonymous]
  19.     public class LoginModel : PageModel
  20.     {
  21.         private readonly UserManager<CinemaWorldUser> _userManager;
  22.         private readonly SignInManager<CinemaWorldUser> _signInManager;
  23.         private readonly ILogger<LoginModel> _logger;
  24.  
  25.         public LoginModel(SignInManager<CinemaWorldUser> signInManager,
  26.             ILogger<LoginModel> logger,
  27.             UserManager<CinemaWorldUser> userManager)
  28.         {
  29.             _userManager = userManager;
  30.             _signInManager = signInManager;
  31.             _logger = logger;
  32.         }
  33.  
  34.         [BindProperty]
  35.         public InputModel Input { get; set; }
  36.  
  37.         public IList<AuthenticationScheme> ExternalLogins { get; set; }
  38.  
  39.         public string ReturnUrl { get; set; }
  40.  
  41.         [TempData]
  42.         public string ErrorMessage { get; set; }
  43.  
  44.         public class InputModel
  45.         {
  46.             [Required]
  47.             [EmailAddress]
  48.             public string Email { get; set; }
  49.  
  50.             [Required]
  51.             [DataType(DataType.Password)]
  52.             public string Password { get; set; }
  53.  
  54.             [Display(Name = "Remember me?")]
  55.             public bool RememberMe { get; set; }
  56.         }
  57.  
  58.         public async Task OnGetAsync(string returnUrl = null)
  59.         {
  60.             if (!string.IsNullOrEmpty(ErrorMessage))
  61.             {
  62.                 ModelState.AddModelError(string.Empty, ErrorMessage);
  63.             }
  64.  
  65.             returnUrl = returnUrl ?? Url.Content("~/");
  66.  
  67.             // Clear the existing external cookie to ensure a clean login process
  68.             await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
  69.  
  70.             ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
  71.  
  72.             ReturnUrl = returnUrl;
  73.         }
  74.  
  75.         public async Task<IActionResult> OnPostAsync(string returnUrl = null)
  76.         {
  77.             returnUrl = returnUrl ?? Url.Content("~/");
  78.  
  79.             if (ModelState.IsValid)
  80.             {
  81.                 // This doesn't count login failures towards account lockout
  82.                 // To enable password failures to trigger account lockout, set lockoutOnFailure: true
  83.                 var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);
  84.                 if (result.Succeeded)
  85.                 {
  86.                     _logger.LogInformation("User logged in.");
  87.                     return LocalRedirect(returnUrl);
  88.                 }
  89.                 if (result.RequiresTwoFactor)
  90.                 {
  91.                     return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe });
  92.                 }
  93.                 if (result.IsLockedOut)
  94.                 {
  95.                     _logger.LogWarning("User account locked out.");
  96.                     return RedirectToPage("./Lockout");
  97.                 }
  98.                 else
  99.                 {
  100.                     ModelState.AddModelError(string.Empty, "Invalid login attempt.");
  101.                     return Page();
  102.                 }
  103.             }
  104.  
  105.             // If we got this far, something failed, redisplay form
  106.             return Page();
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement