public void Validate(LoginMessages.Request request) { var username = request.Username; var password = request.Password; if (String.IsNullOrEmpty(username) == true) { throw LoginValidationException.CreateException(LoginValidationExceptions.InvalidUsername); } if (String.IsNullOrEmpty(password) == true) { throw LoginValidationException.CreateException(LoginValidationExceptions.InvalidPassword); } }
public LoginMessages.Response Handle(LoginMessages.Request request) { // Validate request try { Validator.Validate(request); } catch (LoginValidationException ex) { var errorResponse = new LoginMessages.Response() { Success = false, Exception = ex }; return(errorResponse); } // Authenticate Account Account account; try { account = AccountGateway.GetAccount(request.Username); if (account == null) { throw LoginException.Create(LoginExceptions.IncorrectCredentials); } if (account.Password != request.Password) { throw LoginException.Create(LoginExceptions.IncorrectCredentials); } } catch (LoginException ex) { var errorResponse = new LoginMessages.Response() { Success = false, Exception = ex }; return(errorResponse); } var sessionString = SessionCreator.CreateSession(); var session = new Session { Id = sessionString, PlayerId = account.PlayerId, GameId = null }; try { SessionGateway.CreateSession(session); } catch (Exception e) { var message = e.Message; var errorResponse = new LoginMessages.Response() { Success = false, Exception = new Exception("Could not create a session! Error: " + message) }; return(errorResponse); } var response = new LoginMessages.Response() { Success = true, Session = sessionString, Exception = null }; return(response); }