public QueryResult<string> Logout(int userId, string st) { QueryResult<string> result = new QueryResult<string>(); try { if (userId <=0) { var err = new List<Message>(); err.Add(new Message { Code = Convert.ToInt32(Enums.MessageCode.WarningMessage), Text = "User id should be greater than zero" }); result.InfoMessages = err; result.IsOperationSuccessful = false; result.IsResult = false; result.Result = null; return result; } if (string.IsNullOrEmpty(st)) { var err = new List<Message>(); err.Add(new Message { Code = Convert.ToInt32(Enums.MessageCode.WarningMessage), Text = "Session token is empty" }); result.InfoMessages = err; result.IsOperationSuccessful = false; result.IsResult = false; result.Result = null; return result; } if (!SessionHelper.IsValidSession(st)) { var err = new List<Message>(); err.Add(new Message { Code = Convert.ToInt32(Enums.MessageCode.WarningMessage), Text = "Invalid Session Token" }); result.InfoMessages = err; result.IsOperationSuccessful = false; result.IsResult = false; result.Result = null; return result; } using (var userLoginSessionService = new UserLoginSessionService()) { var item = userLoginSessionService.GetSingle(x => x.UserId == userId && x.SessionToken == st); if(item !=null) { //item.SessionToken = null; //item.UpdatedOn = DateTime.Now; userLoginSessionService.Delete(item); result.IsOperationSuccessful = false; result.IsResult = false; result.Result = "User logged out successfully"; } else { var err = new List<Message>(); err.Add(new Message { Code = Convert.ToInt32(Enums.MessageCode.WarningMessage), Text = "User login session object not found" }); result.InfoMessages = err; result.IsOperationSuccessful = false; result.IsResult = false; result.Result = null; return result; } } } catch(Exception ex) { var err = new List<Message>(); err.Add(new Message { Code = Convert.ToInt32(Enums.MessageCode.ErrorMessage), Text = ex.Message }); result.ErrorMessages = err; result.IsResult = false; result.IsOperationSuccessful = false; result.Result = null; } return result; }
public QueryResult<UserResponse> Login(string userName, string password) { int userStatus = -1; QueryResult<UserResponse> result = new QueryResult<UserResponse>(); try { if (string.IsNullOrEmpty(userName)) { var err = new List<Message>(); err.Add(new Message { Code = Convert.ToInt32(Enums.MessageCode.WarningMessage), Text = "User name is empty" }); result.InfoMessages = err; result.IsOperationSuccessful = false; result.IsResult = false; result.Result = null; return result; } if (string.IsNullOrEmpty(password)) { var err = new List<Message>(); err.Add(new Message { Code = Convert.ToInt32(Enums.MessageCode.WarningMessage), Text = "Password is empty" }); result.InfoMessages = err; result.IsOperationSuccessful = false; result.IsResult = false; result.Result = null; return result; } userStatus = Convert.ToInt32(Enums.UserStatus.Active); using(var userService = new UserService()) { var user = userService.GetSingle(x => x.Status == userStatus); if(user !=null) { // Insert session token into UserLoginSession table string sessionkey = CommonHelper.GetBase64EncodedString(user.Id.ToString()); using (var userLoginService = new UserLoginSessionService()) { var item = userLoginService.GetSingle(x => x.UserId == user.Id); if (item != null) { item.SessionToken = sessionkey; item.UpdatedOn = DateTime.Now; userLoginService.Update(item); } else { UserLoginSession userLoginSession = new UserLoginSession() { CreatedOn = DateTime.Now, DeviceMacId = sessionkey.Substring(0, 10), SessionToken = sessionkey, Status = Convert.ToInt32(Enums.UserLoginSessionStatus.Active), UserId = user.Id }; new UserLoginSessionService().Insert(userLoginSession); } } var userResponse = userService.ToModelResponse(user); if(userResponse !=null) { userResponse.SessionToken = sessionkey; result.IsOperationSuccessful = true; result.IsResult = true; result.Result = userResponse; } else { var err = new List<Message>(); err.Add(new Message { Code = Convert.ToInt32(Enums.MessageCode.ErrorMessage), Text = "Failed to get user respone model" }); result.ErrorMessages = err; result.IsResult = false; result.IsOperationSuccessful = true; result.Result = null; } } else { var err = new List<Message>(); err.Add(new Message { Code = Convert.ToInt32(Enums.MessageCode.ErrorMessage), Text = "User not found" }); result.ErrorMessages = err; result.IsResult = false; result.IsOperationSuccessful = true; result.Result = null; } } } catch(Exception ex) { var err = new List<Message>(); err.Add(new Message { Code = Convert.ToInt32(Enums.MessageCode.ErrorMessage), Text = ex.Message }); result.ErrorMessages = err; result.IsResult = false; result.IsOperationSuccessful = false; result.Result = null; } return result; }