public UserSessionDto Login(string userId, string password) { if (string.IsNullOrEmpty(userId)) { throw new ArgumentException("User Id cannot be null", nameof(userId)); } if (string.IsNullOrEmpty(password)) { throw new ArgumentException("Password cannot be null", nameof(password)); } UserLogin userLoginEntity = null; using (UserLoginRepository userLoginRepository = new UserLoginRepository()) { string securePassword = password.Encrypt(); userLoginEntity = userLoginRepository.Find(x => x.UserId == userId && x.Password == securePassword); if (userLoginEntity == null) { throw new ApplicationException("Invalid UserId/Password"); } } using (UserInfoRepository repository = new UserInfoRepository()) { UserInfo userInfo = repository.Find(x => x.UserId == userId); if (userInfo == null) { throw new ApplicationException("User Info not found."); } string sessionId = Guid.NewGuid().ToString(); using (UserSessionRepository userSessionRepository = new UserSessionRepository()) { UserSession userSession = new UserSession { UserId = userId, SessionId = sessionId, ValidFrom = DateTime.Now, ExpiresOn = DateTime.Now.AddDays(1) }; userSessionRepository.Insert(userSession); } return(new UserSessionDto() { SessionId = sessionId, User = new UserInfoDto() { FirstName = userInfo.FirstName, LastName = userInfo.LastName, Email = userInfo.EMail, UserId = userInfo.UserId, Gender = userInfo.Gender } }); } }
public int RegisterUser(UserRegistrationDto userRegistrationDto) { UserLoginDto userLogin = userRegistrationDto.LoginInfo; UserInfoDto userInfo = userRegistrationDto.UserInfo; int userId; try { if (userLogin == null) { throw new ArgumentNullException("User Login Information cannot be null"); } if (userInfo == null) { throw new ArgumentNullException("User Information cannot be null"); } using (UserLoginRepository userLoginRepository = new UserLoginRepository()) { UserLogin userLoginEntity = userLoginRepository.Find(x => x.EmployeeId == userLogin.EmployeeId); if (userLoginEntity != null) { throw new Exception("User with same employee id already exists."); } else { userLoginEntity = new UserLogin() { EmployeeId = userLogin.EmployeeId, Password = userLogin.Password.Encrypt(), IsActive = true, IsLocked = false, RetryCount = 0 }; userId = userLoginRepository.Insert(userLoginEntity); } } if (userId != 0) { using (UserInfoRepository userInfoRepository = new UserInfoRepository()) { UserInfo userInfoEntity = new UserInfo() { EmployeeId = userInfo.EmployeeId, EMail = userInfo.Email, FirstName = userInfo.FirstName, LastName = userInfo.LastName, Gender = userInfo.Gender }; return(userInfoRepository.Insert(userInfoEntity)); } } else { throw new Exception("Failed to register user."); } } catch (Exception ex) { logger.Error(ex); throw; } }