Guest User

Untitled

a guest
Jun 29th, 2018
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. [HttpPost]
  2. [AllowAnonymous]
  3. [ValidateAntiForgeryToken]
  4. public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
  5. {
  6. ViewData["ReturnUrl"] = returnUrl;
  7. if (ModelState.IsValid)
  8. {
  9. // This doesn't count login failures towards account lockout
  10. // To enable password failures to trigger account lockout, set lockoutOnFailure: true
  11. var result = await _signInManager.PasswordSignInAsync(model.Login, model.Password, model.RememberMe, lockoutOnFailure: false);
  12. if (result.Succeeded)
  13. {
  14. _logger.LogInformation("User logged in.");
  15. return RedirectToLocal(returnUrl);
  16. }
  17. if (result.IsLockedOut)
  18. {
  19. _logger.LogWarning("User account locked out.");
  20. return RedirectToAction(nameof(Lockout));
  21. }
  22. else
  23. {
  24. ModelState.AddModelError(string.Empty, "Connection failed.");
  25. return View(model);
  26. }
  27. }
  28. return View(model);
  29. }
  30.  
  31. public class CustomUserManager<TUser> : UserManager<TUser> where TUser : User
  32. {
  33. public CustomUserManager(IUserStore<TUser> store, IOptions<IdentityOptions> optionsAccessor,
  34. IPasswordHasher<TUser> passwordHasher, IEnumerable<IUserValidator<TUser>> userValidators,
  35. IEnumerable<IPasswordValidator<TUser>> passwordValidators, ILookupNormalizer keyNormalizer,
  36. IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<TUser>> logger) : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
  37. {
  38. }
  39.  
  40. public override Task<bool> CheckPasswordAsync(TUser user, string password)
  41. {
  42. bool passwordIsSimilar = password == user.MotDePasse;
  43.  
  44. return new Task<bool>(() => passwordIsSimilar);
  45. }
  46. }
  47.  
  48. services.AddIdentity<User, ProfileUser>().AddUserManager<CustomUserManager<User>>().AddDefaultTokenProviders();
  49. services.AddTransient<IUserStore<User>, UserIdentity>();
  50. services.AddTransient<IRoleStore<ProfileUser>, ProfileIdentity>();
  51.  
  52. services.Configure<IdentityOptions>(options =>
  53. {
  54. // Lockout settings
  55. options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
  56. options.Lockout.MaxFailedAccessAttempts = 10;
  57. options.Lockout.AllowedForNewUsers = true;
  58. });
  59.  
  60. services.ConfigureApplicationCookie(options =>
  61. {
  62. // Cookie settings
  63. options.Cookie.HttpOnly = true;
  64. options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
  65. // If the LoginPath isn't set, ASP.NET Core defaults
  66. // the path to /Account/Login.
  67. options.LoginPath = "/Account/Login";
  68. // If the AccessDeniedPath isn't set, ASP.NET Core defaults
  69. // the path to /Account/AccessDenied.
  70. options.AccessDeniedPath = "/Account/AccessDenied";
  71. options.SlidingExpiration = true;
  72. });
Add Comment
Please, Sign In to add comment