コード例 #1
0
        protected virtual async Task SaveLoginAttempt(StudioXLoginResult <TTenant, TUser> loginResult, string tenancyName, string userNameOrEmailAddress)
        {
            using (var uow = UnitOfWorkManager.Begin(TransactionScopeOption.Suppress))
            {
                var tenantId = loginResult.Tenant != null ? loginResult.Tenant.Id : (int?)null;
                using (UnitOfWorkManager.Current.SetTenantId(tenantId))
                {
                    var loginAttempt = new UserLoginAttempt
                    {
                        TenantId    = tenantId,
                        TenancyName = tenancyName,

                        UserId = loginResult.User != null ? loginResult.User.Id : (long?)null,
                        UserNameOrEmailAddress = userNameOrEmailAddress,

                        Result = loginResult.Result,

                        BrowserInfo     = ClientInfoProvider.BrowserInfo,
                        ClientIpAddress = ClientInfoProvider.ClientIpAddress,
                        ClientName      = ClientInfoProvider.ComputerName,
                    };

                    await UserLoginAttemptRepository.InsertAsync(loginAttempt);

                    await UnitOfWorkManager.Current.SaveChangesAsync();

                    await uow.CompleteAsync();
                }
            }
        }
コード例 #2
0
        private async Task SaveLoginAttempt(AbpLoginResult <SysTenant, SysUser> loginResult, string tenancyName, string userNameOrEmailAddress)
        {
            using (var uow = UnitOfWorkManager.Begin(TransactionScopeOption.Suppress))
            {
                var tenantId = loginResult.Tenant?.Id;
                using (UnitOfWorkManager.Current.SetTenantId(tenantId))
                {
                    var loginAttempt = new UserLoginAttempt
                    {
                        TenantId    = tenantId,
                        TenancyName = tenancyName,

                        UserId = loginResult.User?.Id,
                        UserNameOrEmailAddress = userNameOrEmailAddress,

                        Result = loginResult.Result,

                        BrowserInfo     = ClientInfoProvider.BrowserInfo,
                        ClientIpAddress = ClientInfoProvider.ClientIpAddress,
                        ClientName      = ClientInfoProvider.ComputerName,
                    };

                    await UserLoginAttemptRepository.InsertAsync(loginAttempt);

                    await UnitOfWorkManager.Current.SaveChangesAsync();

                    await uow.CompleteAsync();
                }
            }
        }
コード例 #3
0
        public IActionResult Post([FromBody] UserLoginAttempt loginAttempt)
        {
            if (!ModelState.IsValid)
            {
                BadRequest(ModelState);
            }

            if (PasswordChecker.PasswordIsValid(loginAttempt))
            {
                var userAccountManager = new UserAccountManager();
                var userAccount        = userAccountManager.GetByUserName(loginAttempt.UserName);

                var lastLogin = userAccount.LastLogin;

                userAccount.LastLogin = DateTime.UtcNow;

                userAccountManager.Update(userAccount);

                var timeSpan = userAccount.LastLogin - lastLogin;

                return(Ok($"Last successful login: {timeSpan.Days} days, {timeSpan.Hours} hours, {timeSpan.Minutes} minutes, and {timeSpan.Seconds} seconds."));
            }
            else
            {
                return(Unauthorized());
            }
        }
コード例 #4
0
        private async Task SaveLoginAttempt(AbpLoginResult loginResult, string userNameOrEmailAddress)
        {
            using (var uow = _unitOfWorkManager.Begin(TransactionScopeOption.Suppress))
            {
                var loginAttempt = new UserLoginAttempt
                {
                    UserId   = loginResult.User != null ? loginResult.User.Id : (long?)null,
                    UserName = userNameOrEmailAddress,

                    Result = loginResult.Result,
                };

                //TODO: We should replace this workaround with IClientInfoProvider when it's implemented in ABP (https://github.com/aspnetboilerplate/aspnetboilerplate/issues/926)
                if (AuditInfoProvider != null)
                {
                    var auditInfo = new AuditInfo();
                    AuditInfoProvider.Fill(auditInfo);
                    loginAttempt.BrowserInfo     = auditInfo.BrowserInfo;
                    loginAttempt.ClientIpAddress = auditInfo.ClientIpAddress;
                    loginAttempt.ClientName      = auditInfo.ClientName;
                }

                await _userLoginAttemptRepository.InsertAsync(loginAttempt);

                await _unitOfWorkManager.Current.SaveChangesAsync();

                await uow.CompleteAsync();
            }
        }
コード例 #5
0
ファイル: UserService.cs プロジェクト: Morr0/Atheer
        private async Task AddLoginAttempt(User user, DateTime time, bool successfulLogin)
        {
            var loginAttempt = new UserLoginAttempt
            {
                UserId          = user.Id,
                AttemptAt       = time,
                SuccessfulLogin = successfulLogin
            };

            await _context.UserLoginAttempt.AddAsync(loginAttempt).CAF();
        }
コード例 #6
0
 public async Task InsertIntoUserLoginAttemptsAsync(UserLoginAttempt userLoginAttempt)
 {
     try
     {
         using (var db = new FutsalEntities())
         {
             db.UserLoginAttempts.Add(userLoginAttempt);
             await db.SaveChangesAsync();
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
コード例 #7
0
        protected virtual async Task SaveLoginAttempt(SmdLoginResult <TUser> loginResult, string userNameOrEmailAddress)
        {
            var loginAttempt = new UserLoginAttempt
            {
                UserId = loginResult.User != null ? loginResult.User.Id : (long?)null,
                UserNameOrEmailAddress = userNameOrEmailAddress,

                Result = loginResult.Result,

                BrowserInfo     = ClientInfoProvider.BrowserInfo,
                ClientIpAddress = ClientInfoProvider.ClientIpAddress,
                ClientName      = ClientInfoProvider.ComputerName,
            };

            await UserLoginAttemptRepository.InsertAsync(loginAttempt);
        }
コード例 #8
0
        protected virtual async Task SaveLoginAttempt(LoginResult <TUser> loginResult, string userNameOrEmailAddress)
        {
            var loginAttempt = new UserLoginAttempt
            {
                UserId = loginResult.User != null ? loginResult.User.Id : (long?)null,
                UserNameOrEmailAddress = userNameOrEmailAddress,

                Result = loginResult.Result,

                BrowserInfo     = ClientInfoProvider.BrowserInfo,
                ClientIpAddress = ClientInfoProvider.ClientIpAddress,
                ClientName      = ClientInfoProvider.ComputerName,
            };

            await UserLoginAttemptRepository.InsertAsync(loginAttempt);

            _repositoryContext.Commit();
            //await UnitOfWorkManager.Current.SaveChangesAsync();

            //await uow.CompleteAsync();
        }
コード例 #9
0
        protected virtual async Task SaveLoginAttempt(EddoLoginResult <TUser> loginResult, string tenancyName, string userNameOrEmailAddress)
        {
            using (var uow = UnitOfWorkManager.Begin())
            {
                var loginAttempt = new UserLoginAttempt
                {
                    TenantId    = null,
                    TenancyName = tenancyName,

                    UserId = loginResult.User != null ? loginResult.User.Id : (long?)null,
                    UserNameOrEmailAddress = userNameOrEmailAddress,

                    Result = loginResult.Result,

                    BrowserInfo     = ClientInfoProvider.BrowserInfo,
                    ClientIpAddress = ClientInfoProvider.ClientIpAddress,
                    ClientName      = ClientInfoProvider.ComputerName,
                };
                await UserLoginAttemptRepository.AddAsync(loginAttempt);

                await uow.CommitAsync();
            }
        }
コード例 #10
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _userAccountManager.GetUserAsync(model.Email);

            if (user == null)
            {
                return(RedirectToAction(nameof(Login)));
            }

            string hashedPassword = new PasswordHasher().HashPassword(model.Password);

            #region User login Attempt History

            var userLoginAttempt = new UserLoginAttempt
            {
                UserId          = user.Id,
                PasswordHash    = hashedPassword,
                ApplicationId   = await _applicationManager.ReturnApplicationIDAsync(),
                AttemptDateTime = DateTime.Now,
                IPAddress       = _applicationManager.ReturnIPAddress()
            };

            if (!user.IsUserActive)//user is inactive
            {
                userLoginAttempt.Reason       = "User Inactive";
                userLoginAttempt.IsSuccessful = false;
                await _userAccountManager.InsertIntoUserLoginAttemptsAsync(userLoginAttempt); //insert into login attempts

                return(RedirectToAction("UserInactive", "Error"));
            }
            else // user active
            {
                userLoginAttempt.Reason       = "User Active Loggin";
                userLoginAttempt.IsSuccessful = true;
                await _userAccountManager.InsertIntoUserLoginAttemptsAsync(userLoginAttempt);

                var userHistory = new UserHistory {
                    UserId = user.Id, Application = await _applicationManager.ReturnApplicationNameAsync(), MachineName = _applicationManager.ReturnIPAddress(), Operation = "", TimeStamp = DateTime.Now
                };

                await _userAccountManager.InsertIntoUserHistoryAsync(userHistory);
            }
            #endregion

            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, change to shouldLockout: true
            try
            {
                var result = await _signInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberMe, shouldLockout : false);

                switch (result)
                {
                case SignInStatus.Success:
                    return(RedirectToLocal(returnUrl));

                case SignInStatus.LockedOut:
                    return(View("Lockout"));

                case SignInStatus.RequiresVerification:
                    return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }));

                case SignInStatus.Failure:
                default:
                    ModelState.AddModelError("", "Invalid login attempt.");
                    return(View(model));
                }
            }
            catch (Exception ex)
            {
                // throw ex;
            }
            return(View(model));
        }
コード例 #11
0
 public async Task InsertIntoUserLoginAttemptsAsync(UserLoginAttempt userLoginAttempt) => await _userAccountRepository.InsertIntoUserLoginAttemptsAsync(userLoginAttempt);