public LoginResponse Login([FromBody] LoginRequest data) { if (string.IsNullOrEmpty(data.userName)) { throw ModelException("userName", AuthMessage.UserNameMustHaveAValue.NiceToString()); } if (string.IsNullOrEmpty(data.password)) { throw ModelException("password", AuthMessage.PasswordMustHaveAValue.NiceToString()); } // Attempt to login UserEntity user = null; try { user = AuthLogic.Login(data.userName, Security.EncodePassword(data.password)); } catch (Exception e) when(e is IncorrectUsernameException || e is IncorrectPasswordException) { if (AuthServer.MergeInvalidUsernameAndPasswordMessages) { ModelState.AddModelError("userName", AuthMessage.InvalidUsernameOrPassword.NiceToString()); ModelState.AddModelError("password", AuthMessage.InvalidUsernameOrPassword.NiceToString()); throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, this.ModelState)); } else if (e is IncorrectUsernameException) { throw ModelException("userName", AuthMessage.InvalidUsername.NiceToString()); } else if (e is IncorrectPasswordException) { throw ModelException("password", AuthMessage.InvalidPassword.NiceToString()); } } catch (IncorrectPasswordException) { throw ModelException("password", AuthServer.MergeInvalidUsernameAndPasswordMessages ? AuthMessage.InvalidUsernameOrPassword.NiceToString() : AuthMessage.InvalidPassword.NiceToString()); } using (UserHolder.UserSession(user)) { if (data.rememberMe == true) { UserTicketServer.SaveCookie(); } AuthServer.AddUserSession(user); string message = AuthLogic.OnLoginMessage(); var token = AuthTokenServer.CreateToken(user); return(new LoginResponse { message = message, userEntity = user, token = token }); } }
public ActionResult <LoginResponse> Login([Required, FromBody] LoginRequest data) { if (string.IsNullOrEmpty(data.userName)) { return(ModelError("userName", LoginAuthMessage.UserNameMustHaveAValue.NiceToString())); } if (string.IsNullOrEmpty(data.password)) { return(ModelError("password", LoginAuthMessage.PasswordMustHaveAValue.NiceToString())); } string authenticationType; // Attempt to login UserEntity user; try { if (AuthLogic.Authorizer == null) { user = AuthLogic.Login(data.userName, Security.EncodePassword(data.password), out authenticationType); } else { user = AuthLogic.Authorizer.Login(data.userName, data.password, out authenticationType); } } catch (Exception e) when(e is IncorrectUsernameException || e is IncorrectPasswordException) { if (AuthServer.MergeInvalidUsernameAndPasswordMessages) { return(ModelError("login", LoginAuthMessage.InvalidUsernameOrPassword.NiceToString())); } else if (e is IncorrectUsernameException) { return(ModelError("userName", LoginAuthMessage.InvalidUsername.NiceToString())); } else if (e is IncorrectPasswordException) { return(ModelError("password", LoginAuthMessage.InvalidPassword.NiceToString())); } throw; } catch (Exception e) { return(ModelError("login", e.Message)); } AuthServer.OnUserPreLogin(ControllerContext, user); AuthServer.AddUserSession(ControllerContext, user); if (data.rememberMe == true) { UserTicketServer.SaveCookie(ControllerContext); } var token = AuthTokenServer.CreateToken(user); return(new LoginResponse { userEntity = user, token = token, authenticationType = authenticationType }); }