public async Task IpAddress_HandleFailedLoginAttempt_RemoveOldEntriesIsCalled()
        {
            await _service.HandleFailedLoginAttempt("1.2.3.4");

            _mockRepository.Verify(r => r.RemoveAnyOldEntries(It.IsAny <string>(), It.IsAny <DateTimeOffset>()), Times.Once);
        }
Пример #2
0
        public async Task <IActionResult> Login(LoginModel model, string returnUrl)
        {
            var ipAddress = HttpContext.Connection.RemoteIpAddress;

            ViewData["TooManyRequests"] = false;

            if (model.Email == null || model.Password == null)
            {
                return(View(model));
            }

            if (ipAddress == null)
            {
                throw new IpAddressNotFoundException();
            }

            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                _trackTelemetry.TrackEvent(EventName.Login, EventType.Action, EventStatus.UserNotFound);

                ModelState.AddModelError(string.Empty, _messageConstants.InvalidLoginAttempt);

                await _failedLoginAttemptsService.HandleFailedLoginAttempt(ipAddress.ToString());

                var tooManyRequestsForIpAddress = await _failedLoginAttemptsService.HasTooManyRequestsForIpAddressAsync(ipAddress.ToString());

                if (tooManyRequestsForIpAddress)
                {
                    return(RedirectToAction(nameof(Login)));
                }

                return(View(model));
            }

            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                await _failedLoginAttemptsService.HandleFailedLoginAttempt(ipAddress.ToString());

                var tooManyRequestsForIpAddress = await _failedLoginAttemptsService.HasTooManyRequestsForIpAddressAsync(ipAddress.ToString());

                if (tooManyRequestsForIpAddress)
                {
                    return(RedirectToAction(nameof(Login)));
                }

                var result = await _signInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberMe, true);

                if (result.Succeeded)
                {
                    _trackTelemetry.TrackEvent(EventName.Login, EventType.Action, EventStatus.Success);
                    return(RedirectToLocal(returnUrl));
                }

                if (result.IsLockedOut)
                {
                    await SendLockoutEmail(user);

                    _trackTelemetry.TrackEvent(EventName.Login, EventType.Action, EventStatus.Lockout);
                    _logger.LogWarning("The user has been locked out after too many failed login attempts");
                }

                if (result.IsNotAllowed)
                {
                    _trackTelemetry.TrackEvent(EventName.Login, EventType.Action, EventStatus.NotVerified);
                }

                ModelState.AddModelError(string.Empty, _messageConstants.InvalidLoginAttempt);
            }

            _trackTelemetry.TrackEvent(EventName.Login, EventType.Action, EventStatus.Fail);

            return(View(model));
        }