ASP.NET MVC Session Expiration

前端 未结 6 2018
孤街浪徒
孤街浪徒 2021-01-01 21:51

We have an internal ASP.NET MVC application that requires a logon. Log on works great and does what\'s expected. We have a session expiration of 15 minutes. After sitting on

6条回答
  •  無奈伤痛
    2021-01-01 22:03

    Here's how I did it...

    In my base controller

     protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                if (filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    filterContext.HttpContext.Response.StatusCode = 403;
                    filterContext.HttpContext.Response.Write(SessionTimeout);
                    filterContext.HttpContext.Response.End();
                }
            }
        }
    

    Then in my global .js file

    $.ajaxSetup({
    error: function (x, status, error) {
        if (x.status == 403) {
            alert("Sorry, your session has expired. Please login again to continue");
            window.location.href = "/Account/Login";
        }
        else {
            alert("An error occurred: " + status + "nError: " + error);
        }
    }
    

    });

    The SessionTimeout variable is a noty string. I omitted the implementation for brevity.

提交回复
热议问题