public override void Handle(ExceptionHandlerContext context) { base.Handle(context); if (context.Exception is FException) { var result = new SysApiResult <string>() { Status = SysApiStatus.失败, Message = context.Exception.Message }; context.Result = new ResponseMessageResult(context.Request.CreateResponse(HttpStatusCode.InternalServerError, result)); } else { var result = new SysApiResult <string>() { Status = SysApiStatus.异常, Message = "服务器繁忙,请稍候再试" }; context.Result = new ResponseMessageResult(context.Request.CreateResponse(HttpStatusCode.InternalServerError, result)); } }
public void OnException(ExceptionContext executedContext) { executedContext.ExceptionHandled = true; bool isAjaxRequest = executedContext.HttpContext.Request.IsAjaxRequest(); if (isAjaxRequest && executedContext.HttpContext.Request.Query["NeedLayout"] == "false") { //如果是不需要母版页的ajax请求获取页面Html内容,不做处理,让ajax的error function()来处理 return; } SysApiResult <string> result; if (executedContext.Exception is FException) { result = new SysApiResult <string>() { Status = SysApiStatus.失败, Message = executedContext.Exception.Message }; } else if (executedContext.Exception is AntiforgeryValidationException) { result = new SysApiResult <string>() { Status = SysApiStatus.拦截, Message = "服务器繁忙,请重新登陆。" }; } else { logger.Error(executedContext.Exception, "全局异常捕获"); result = new SysApiResult <string>() { Status = SysApiStatus.异常, Message = "服务器繁忙,请稍候再试" }; } if (isAjaxRequest) { executedContext.Result = new JsonNetResult(value: result); } else { string errorRedirect = _configuration?["appSettings:ErrorRedirect"]; if (!string.IsNullOrWhiteSpace(errorRedirect)) { if (executedContext.Exception is FException) { errorRedirect = $"{errorRedirect}?message={executedContext.Exception.Message}"; } executedContext.Result = new RedirectResult(errorRedirect); } else { executedContext.Result = new JsonNetResult(value: result); } } }
public override void OnActionExecuting(ActionExecutingContext actionContext) { base.OnActionExecuting(actionContext); if (actionContext.ActionParameters.Count > 0) { var model = actionContext.ActionParameters.First().Value; if (model == null) { var result = new SysApiResult <string>() { Status = SysApiStatus.未授权, Message = "请求参数不能为空!" }; actionContext.Result = new JsonNetResult { Data = result }; return; } } var currController = actionContext.Controller as BaseController; if (currController?.ModelState?.IsValid == false) { ModelError firstError = new ModelError("未知错误"); for (int i = 0; i < currController.ModelState.Keys.Count; i++) { var errorList = currController.ModelState.Values.ElementAt(i).Errors; if (errorList == null || errorList.Count <= 0) { continue; } firstError = errorList.First(); break; } string errorMsg = string.IsNullOrWhiteSpace(firstError.ErrorMessage) ? firstError.Exception?.Message ?? "" : firstError.ErrorMessage; var result = new SysApiResult <string>() { Status = SysApiStatus.异常, Message = errorMsg }; actionContext.Result = new JsonNetResult { Data = result }; return; } }
/// <summary> /// 在操作执行之前、模型绑定完成后调用。 /// </summary> public void OnActionExecuting(ActionExecutingContext actionContext) { if (actionContext.ActionArguments.Count > 0) { var model = actionContext.ActionArguments.First().Value; if (model == null) { var result = new SysApiResult <string>() { Status = SysApiStatus.未授权, Message = "请求参数不能为空!" }; actionContext.Result = new JsonNetResult(value: result); return; } } if (!actionContext.ModelState.IsValid) { ModelError firstError = new ModelError("未知错误"); for (int i = 0; i < actionContext.ModelState.Keys.Count(); i++) { var errorList = actionContext.ModelState.Values.ElementAt(i).Errors; if (errorList == null || errorList.Count <= 0) { continue; } firstError = errorList.First(); break; } string errorMsg = string.IsNullOrWhiteSpace(firstError.ErrorMessage) ? firstError.Exception?.Message ?? "" : firstError.ErrorMessage; var result = new SysApiResult <string>() { Status = SysApiStatus.异常, Message = errorMsg }; actionContext.Result = new JsonNetResult(value: result); return; } }
protected virtual IUser GetUser(HttpActionContext actionContext) { var accessToken = string.Empty; if (!actionContext.Request.TryGetToken(_accessTokenKey, out accessToken)) { var result = new SysApiResult <string>() { Status = SysApiStatus.未授权, Message = "您的登陆身份已过期,请重新登陆" }; actionContext.Response = actionContext.Request.CreateResponse(result); return(null); } UserModel userModel = GetUserModelForCache(accessToken); //从数据库获取客户信息 if (userModel == null) { var result = new SysApiResult <string>() { Status = SysApiStatus.未授权, Message = "您的登陆身份已过期,请重新登陆" }; actionContext.Response = actionContext.Request.CreateResponse(result); return(null); } //时间过期 if (userModel.TokenExpireTime <= DateTime.Now) { var result = new SysApiResult <string>() { Status = SysApiStatus.过期, Message = "token已过期,请重新登陆" }; actionContext.Response = actionContext.Request.CreateResponse(result); return(null); } return(userModel); }
/// <summary> /// 重新跳到登陆页面 /// </summary> private void RegirectToLoginUrl(AuthorizationFilterContext filterContext) { if (filterContext.HttpContext.Request.IsAjaxRequest()) { var result = new SysApiResult <string>() { Status = SysApiStatus.未授权, Message = "您的登陆身份已过期,请重新登陆" }; filterContext.Result = new JsonNetResult(value: result); return; } string loginUrl = _configuration?["appSettings:LoginPath"] ?? "/"; filterContext.Result = new RedirectResult(loginUrl); }
/// <summary> /// 重新跳到登陆页面 /// </summary> protected virtual void RegirectToLoginUrl(AuthorizationContext filterContext) { if (filterContext.HttpContext.Request.IsAjaxRequest()) { var result = new SysApiResult<string>() { Status = SysApiStatus.未授权, Message = "您的登陆身份已过期,请重新登陆" }; if (filterContext.HttpContext.Request.HttpMethod.ToLower() == "get") { filterContext.Result = new JsonResult { Data = result, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } else { filterContext.Result = new JsonResult { Data = result }; } return; } filterContext.Result = new RedirectResult(FormsAuthentication.LoginUrl); }
public void OnAuthorization(AuthorizationFilterContext filterContext) { if (filterContext == null) { throw new ArgumentNullException("filterContext"); } if (!filterContext.HttpContext.Request.TryGetHeader(_userTokenKey, out string accessToken)) { var result = new SysApiResult <string>() { Status = SysApiStatus.未授权, Message = "您的登陆身份已过期,请重新登陆" }; filterContext.Result = new JsonNetResult(value: result); return; } var tokenModel = _tokenSvc.DecodeToken(accessToken); //从数据库获取客户信息 if (tokenModel == null) { var result = new SysApiResult <string>() { Status = SysApiStatus.未授权, Message = "您的身份未授权" }; filterContext.Result = new JsonNetResult(value: result); return; } //时间过期 if (tokenModel.TokenExpireTime <= DateTime.Now) { var result = new SysApiResult <string>() { Status = SysApiStatus.未授权, Message = "token已过期,请重新登陆" }; filterContext.Result = new JsonNetResult(value: result); return; } if (!filterContext.HttpContext.Request.TryGetHeader("DeviceType", out string deviceTypeStr) || !Enum.TryParse(deviceTypeStr, out DeviceType deviceType)) { var result = new SysApiResult <string>() { Status = SysApiStatus.异常, Message = "缺失DeviceType" }; filterContext.Result = new JsonNetResult(value: result); return; } bool isOk = _tokenSvc.检查用户登陆是否合法(tokenModel.UserId, deviceType, accessToken, isAdmin: false, isSingleLogin: true, out string errorMsg); if (!isOk) { var result = new SysApiResult <string>() { Status = SysApiStatus.未授权, Message = errorMsg }; filterContext.Result = new JsonNetResult(value: result); return; } }
public void OnException(ExceptionContext actionExecutedContext) { actionExecutedContext.ExceptionHandled = true; bool isAjaxRequest = actionExecutedContext.HttpContext.Request.IsAjaxRequest(); if (isAjaxRequest && actionExecutedContext.HttpContext.Request.QueryString["NeedLayout"] == "false") { //如果是不需要母版页的ajax请求获取页面Html内容,不做处理,让ajax的error function()来处理 return; } SysApiResult <string> result; if (actionExecutedContext.Exception is FException) { result = new SysApiResult <string>() { Status = SysApiStatus.失败, Message = actionExecutedContext.Exception.Message }; } else if (actionExecutedContext.Exception is HttpAntiForgeryException) { result = new SysApiResult <string>() { Status = SysApiStatus.防伪过期, Message = "服务器繁忙,请重新登陆。" }; } else { logger.Error(actionExecutedContext.Exception, "全局异常捕获"); result = new SysApiResult <string>() { Status = SysApiStatus.异常, Message = "服务器繁忙,请稍候再试" }; } if (isAjaxRequest) { if (actionExecutedContext.HttpContext.Request.HttpMethod.ToLower() == "get") { actionExecutedContext.Result = new JsonNetResult { Data = result, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } else { actionExecutedContext.Result = new JsonNetResult { Data = result }; } } else { if (!string.IsNullOrWhiteSpace(_errorRedirect)) { if (actionExecutedContext.Exception is FException) { _errorRedirect = $"{_errorRedirect}?message={actionExecutedContext.Exception.Message}"; } actionExecutedContext.Result = new RedirectResult(_errorRedirect); } else { actionExecutedContext.Result = new JsonNetResult { Data = result }; } } }