ajax调用的会话超时

Ash*_*rma 11 c# asp.net ajax session session-timeout

我知道这是重复但我无法获得可靠的解决方案(对于asp.net web).

如果会话过期,我只想重定向到登录页面.我试过以下:

1.使用jquery状态代码

    $.ajax({
     type: "POST",
     url: "stream.asmx/SomeMethod",
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function (msg) {
        //success msg
     },
     error: function (request, status, error) {
        if (status = 403) {
            location.href = 'login.aspx';
        }
     }
    });
Run Code Online (Sandbox Code Playgroud)

问题:这也为其他错误返回相同的状态代码(403),我只期望会话超时.

2.发送json消息会话是否过期

代码背后:

    if (!object.Equals(HttpContext.Current.Session["User"], null))
    {
        Id = int.Parse(HttpContext.Current.Session["User"].ToString());
    }
    else
    {
        result = from row in dtscrab.AsEnumerable()
                 select new
                 {
                     redirectUrl = "login.aspx",
                     isRedirect = true
                 };
    }
Run Code Online (Sandbox Code Playgroud)

$ .ajax成功:

    success: function (msg) {
        if (msg.d[0].isRedirect) {
            window.location.href = msg.d[0].redirectUrl;
        }
        else {
            //load containt
        }
     }
Run Code Online (Sandbox Code Playgroud)

问题:如果会话过期,它会以某种方式调用ajax成功行(它确实返回正确的json).如果我在页面中有很多ajax请求(应该全局处理),这甚至不是一个正确的方法.

但是,我看到这篇文章真的很好解决了,但它适用于mvc使用AuthorizeAttribute:handling-session-timeout-in-ajax-calls

那么,我是否可以在asp.net web api中使用AuthorizeAttribute在mvc中使用相同的概念?如果没有,我如何解决我面临的问题(上面提到的两个中的任何一个)?

Ash*_*rma 3

最后,我最终还是跟随了。

public class IsAuthorizedAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                var sessions = filterContext.HttpContext.Session;
                if (sessions["User"] != null)
                {
                    return;
                }
                else
                {
                    filterContext.Result = new JsonResult
                    {
                        Data = new
                        {
                            status = "401"
                        },
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };

                    //xhr status code 401 to redirect
                    filterContext.HttpContext.Response.StatusCode = 401;

                    return;
                }
            }

            var session = filterContext.HttpContext.Session;
            if (session["User"] != null)
                return;

            //Redirect to login page.
            var redirectTarget = new RouteValueDictionary { { "action", "LogOn" }, { "controller", "Account" } };
            filterContext.Result = new RedirectToRouteResult(redirectTarget);
        }
    }
Run Code Online (Sandbox Code Playgroud)

处理客户端

<script type="text/javascript">
    $(document).ajaxComplete(
       function (event, xhr, settings) {
           if (xhr.status == 401) {
               window.location.href = "/Account/LogOn";
           }
    });
</script>
Run Code Online (Sandbox Code Playgroud)