public override void OnActionExecuting(ActionExecutingContext context) { base.OnActionExecuting(context); var _dbContext = context.HttpContext.RequestServices.GetService <APIDbContext>(); var accessToken = context.HttpContext.Request.Query[nameof(WithAccessTokenAddressModel.AccessToken)].ToString(); //If we can not find access token from url, we can search it from the request form. if (string.IsNullOrWhiteSpace(accessToken)) { accessToken = context.HttpContext.Request.Form[nameof(WithAccessTokenAddressModel.AccessToken)]; } var target = _dbContext .AccessToken .SingleOrDefault(t => t.Value == accessToken); if (target == null || !target.IsAlive) { var arg = new AiurProtocal { Code = ErrorType.Unauthorized, Message = "We can not validate your access token!" }; context.Result = new JsonResult(arg); } else if (!target.IsAlive) { var arg = new AiurProtocal { Code = ErrorType.Unauthorized, Message = "Your access token is already Timeout!" }; context.Result = new JsonResult(arg); } }
public override void OnException(ExceptionContext context) { base.OnException(context); switch (context.Exception.GetType().Name) { case nameof(NotAiurSignedInException): { var exp = context.Exception as NotAiurSignedInException; var r = context.HttpContext.Request; string ServerPosition = $"{r.Scheme}://{r.Host}"; string url = UrlConverter.UrlWithAuth(ServerPosition, exp.SignInRedirectPath); context.ExceptionHandled = true; context.HttpContext.Response.Redirect(url.ToString()); } break; case nameof(AiurUnexceptedResponse): { var exp = context.Exception as AiurUnexceptedResponse; var arg = new AiurProtocal { code = exp.Response.code, message = exp.Response.message }; var url = new AiurUrl(string.Empty, "api", "exception", arg); context.ExceptionHandled = true; context.HttpContext.Response.Redirect(url.ToString()); } break; case nameof(ModelStateNotValidException): { var exp = context.Exception as ModelStateNotValidException; var arg = new AiurProtocal { code = ErrorType.InvalidInput, message = "Input not valid!" }; var url = new AiurUrl(string.Empty, "api", "exception", arg); context.ExceptionHandled = true; context.HttpContext.Response.Redirect(url.ToString()); } break; default: { var exp = context.Exception as Exception; var arg = new AiurProtocal { code = ErrorType.UnknownError, message = exp.Message }; var url = new AiurUrl(string.Empty, "api", "exception", arg); context.ExceptionHandled = true; context.HttpContext.Response.Redirect(url.ToString()); } break; } }
public override void OnActionExecuting(ActionExecutingContext context) { base.OnActionExecuting(context); var controller = context.Controller as UserController; var accessToken = context.HttpContext.Request.Query[nameof(WithAccessTokenAddressModel.AccessToken)].ToString(); var target = controller._dbContext .AccessToken .SingleOrDefault(t => t.Value == accessToken); if (target == null || !target.IsAlive) { var arg = new AiurProtocal { Code = ErrorType.Unauthorized, Message = "We can not validate your access token!" }; context.Result = new JsonResult(arg); } else if (!target.IsAlive) { var arg = new AiurProtocal { Code = ErrorType.Unauthorized, Message = "Your access token is already Timeout!" }; context.Result = new JsonResult(arg); } }
public override void OnActionExecuting(ActionExecutingContext context) { base.OnActionExecuting(context); if (!context.HttpContext.WebSockets.IsWebSocketRequest) { var arg = new AiurProtocal { Code = ErrorType.InvalidInput, Message = "Wrong protocal!" }; context.Result = new JsonResult(arg); } }
public override void OnException(ExceptionContext context) { base.OnException(context); switch (context.Exception) { case AiurUnexceptedResponse exp: var arg = new AiurProtocal { Code = exp.Response.Code, Message = exp.Response.Message }; context.ExceptionHandled = true; context.Result = new JsonResult(arg); break; } }
public AiurUnexceptedResponse(AiurProtocal response) { Response = response; }
public IActionResult Exception(AiurProtocal model) { return(Json(model)); }