1.过滤器权限验证
1 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)] 2 public class CustomAuthAttribute : ActionFilterAttribute, IAuthorizationFilter 3 { 4 public void OnAuthorization(AuthorizationContext filterContext) 5 { 6 if (!filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) 7 && !filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true)) 8 { 9 UserInfo user = filterContext.HttpContext.Session["CurentUser"] as UserInfo; 10 if (user == null || user.Id <= 0) 11 { 12 if (filterContext.HttpContext.Request.IsAjaxRequest()) 13 { 14 filterContext.Result = new HttpStatusCodeResult(499); 15 filterContext.HttpContext.Response.Write("/Home/Login"); 16 return; 17 } 18 else 19 { 20 filterContext.Result = new RedirectResult("/Home/Login"); 21 return; 22 } 23 } 24 } 25 } 26 }
2.客户端重写$.Ajax,最好在母版页中加上此代码,统一处理
1 <script type="text/javascript"> 2 (function ($) { 3 //备份jquery的ajax方法 4 var _ajax = $.ajax; 5 //重写jquery的ajax方法 6 $.ajax = function (opt) { 7 //备份opt中error和success方法 8 var fn = { 9 error: function (XMLHttpRequest, textStatus, errorThrown) { }, 10 } 11 if (opt.error) { 12 fn.error = opt.error; 13 } 14 //扩展增强处理 15 var _opt = $.extend(opt, { 16 error: function (XMLHttpRequest, textStatus, errorThrown) { 17 //错误方法增强处理 18 if (XMLHttpRequest.status == "499") { 19 window.location.href = "/FlexibleBenefit/Login"; 20 } 21 else 22 { 23 fn.error(XMLHttpRequest, textStatus, errorThrown); 24 } 25 } 26 }); 27 _ajax(_opt); 28 }; 29 })(jQuery); 30 31 $.ajaxSetup({ 32 statusCode: { 33 499: function (data) { 34 window.location.href = data.responseText; 35 } 36 } 37 }); 38 </script>
时间: 2024-11-01 20:36:21