我从 AuthorizeAttribute 类派生并制作了自己的 CustomAuthorize
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.HttpContext.Response.StatusCode = 401;
filterContext.Result = new JsonResult
{
Data = new DataSourceResult { Errors = new { error = "NotAuthorized" } }
,JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
else
{
// this is a standard request, let parent filter to handle it
base.HandleUnauthorizedRequest(filterContext);
}
}
我在很多地方都在使用 Ajax Jquery 调用,并且检查每个地方的响应然后执行
window.location.href='/Auth/login';
我只是想知道,我们可以从 HandleUnauthorizedRequest 方法重定向到登录页面吗?