Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [HttpPost]
- [AllowAnonymous]
- [ValidateAntiForgeryToken]
- public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
- {
- ViewData["ReturnUrl"] = returnUrl;
- if (ModelState.IsValid)
- {
- // This doesn't count login failures towards account lockout
- // To enable password failures to trigger account lockout, set lockoutOnFailure: true
- var result = await _signInManager.PasswordSignInAsync(model.Login, model.Password, model.RememberMe, lockoutOnFailure: false);
- if (result.Succeeded)
- {
- _logger.LogInformation("User logged in.");
- return RedirectToLocal(returnUrl);
- }
- if (result.IsLockedOut)
- {
- _logger.LogWarning("User account locked out.");
- return RedirectToAction(nameof(Lockout));
- }
- else
- {
- ModelState.AddModelError(string.Empty, "Connection failed.");
- return View(model);
- }
- }
- return View(model);
- }
- public class CustomUserManager<TUser> : UserManager<TUser> where TUser : User
- {
- public CustomUserManager(IUserStore<TUser> store, IOptions<IdentityOptions> optionsAccessor,
- IPasswordHasher<TUser> passwordHasher, IEnumerable<IUserValidator<TUser>> userValidators,
- IEnumerable<IPasswordValidator<TUser>> passwordValidators, ILookupNormalizer keyNormalizer,
- IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<TUser>> logger) : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
- {
- }
- public override Task<bool> CheckPasswordAsync(TUser user, string password)
- {
- bool passwordIsSimilar = password == user.MotDePasse;
- return new Task<bool>(() => passwordIsSimilar);
- }
- }
- services.AddIdentity<User, ProfileUser>().AddUserManager<CustomUserManager<User>>().AddDefaultTokenProviders();
- services.AddTransient<IUserStore<User>, UserIdentity>();
- services.AddTransient<IRoleStore<ProfileUser>, ProfileIdentity>();
- services.Configure<IdentityOptions>(options =>
- {
- // Lockout settings
- options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
- options.Lockout.MaxFailedAccessAttempts = 10;
- options.Lockout.AllowedForNewUsers = true;
- });
- services.ConfigureApplicationCookie(options =>
- {
- // Cookie settings
- options.Cookie.HttpOnly = true;
- options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
- // If the LoginPath isn't set, ASP.NET Core defaults
- // the path to /Account/Login.
- options.LoginPath = "/Account/Login";
- // If the AccessDeniedPath isn't set, ASP.NET Core defaults
- // the path to /Account/AccessDenied.
- options.AccessDeniedPath = "/Account/AccessDenied";
- options.SlidingExpiration = true;
- });
Add Comment
Please, Sign In to add comment