예제 #1
0
        public async Task <IActionResult> Login([FromBody] LoginViewModel model)
        {
            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, set lockoutOnFailure: true
            SignInResult result = await this._signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, false);

            if (result.Succeeded)
            {
                ApplicationUser user = await this._userManager.FindByEmailAsync(model.Email);

                IList <string> roles = await this._userManager.GetRolesAsync(user);

                this._logger.LogInformation(1, "User logged in.");
                return(AppUtils.SignIn(user, roles));
            }
            if (result.RequiresTwoFactor)
            {
                return(this.RedirectToAction(nameof(SendCode), new
                {
                    RememberMe = model.RememberMe
                }));
            }
            if (result.IsLockedOut)
            {
                this._logger.LogWarning(2, "User account locked out.");
                return(this.BadRequest(new ApiError("Lockout")));
            }
            return(this.BadRequest(new ApiError("Invalid login attempt.")));
        }
예제 #2
0
        public async Task <IActionResult> Login([FromBody] LoginViewModel model)
        {
            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.Email, model.Password, model.RememberMe, lockoutOnFailure : false);

                if (result.Succeeded)
                {
                    var user = await _userManager.FindByEmailAsync(model.Email);

                    var roles = await _userManager.GetRolesAsync(user);

                    _logger.LogInformation(1, "User logged in.");
                    return(AppUtils.SignIn(user, roles));
                }
                if (result.RequiresTwoFactor)
                {
                    return(RedirectToAction(nameof(SendCode), new { RememberMe = model.RememberMe }));
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning(2, "User account locked out.");
                    return(BadRequest("Lockout"));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return(BadRequest(ModelState.GetModelErrors()));
                }
            }

            return(BadRequest(new string[] { "Unable to login" }));
        }
        public async Task <IActionResult> Login([FromBody] LoginViewModel model)
        {
            if (!_context.Users.Any(x => (x.Email == model.Email)))
            {
                ModelState.AddModelError(string.Empty, "Error while logging in: user with such email does not exist");
                return(BadRequest(ModelState.GetModelErrors()));
            }

            // 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.Email, model.Password, model.RememberMe, lockoutOnFailure : false);

            if (result.Succeeded)
            {
                var user = await _userManager.FindByEmailAsync(model.Email);

                var roles = await _userManager.GetRolesAsync(user);

                _logger.LogInformation(1, "User logged in.");
                return(AppUtils.SignIn(user, roles));
            }
            if (result.RequiresTwoFactor)
            {
                return(RedirectToAction(nameof(SendCode), new { RememberMe = model.RememberMe }));
            }
            if (result.IsLockedOut)
            {
                _logger.LogWarning(2, "User account locked out.");
                return(BadRequest("Lockout"));
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Error while logging in: incorect password.");
                return(BadRequest(ModelState.GetModelErrors()));
            }
        }