public void CreateUserSession(string userId) { string ipAddress = clientContextScraper.GetClientIpAddress(); string userIdentity = aesCryptoHelper.EncryptString(userId); UserSession userSession = new UserSession() { SessionIdentifier = userIdentity, Ip = ipAddress, CreateDt = DateTime.Now }; userSessionRepository.Insert(userSession); userSessionRepository.Save(); }
public UserLoginResponseModel Login(UserLoginModel model) { model.Password = hashCryptoHelper.HashString(model.Password); var data = userDataRepository.FetchAll() .Where(x => x.UserEmail == model.UserEmail && x.PassHash == model.Password) .Select(x => new { x, x.Role.RoleValue }) .FirstOrDefault(); if (data == null) { throw new GenericException(Enums.GenericErrorEnum.UserDoesNotExist); } var userData = data.x; var role = data.RoleValue; userSessionService.DeleteUserSession(userData.Id.ToString()); userSessionService.CreateUserSession(userData.Id.ToString()); if (userData.FailedLoginCount != 0) { userData.FailedLoginCount = 0; userDataRepository.Update(userData); userDataRepository.Save(); } var claims = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, aesCryptoHelper.EncryptString(userData.Id.ToString())), new Claim(ClaimTypes.Role, role.ToString()) }); return(new UserLoginResponseModel() { JWT = jwtHelper.ConstructUserJwt(claims) }); }