如果会话超时,如何在MVC中调用jquery ajax后重定向到新页面?

时间:2022-06-28 20:34:44

1- I have an Ajax link that call an action and that action returns a view , that view open in a specific Div (consider it as a menu that update the div with the corresponding view) 2- if session timeout the returned my logon view

1-我有一个调用动作的Ajax链接,该动作返回一个视图,该视图在特定Div中打开(将其视为用相应视图更新div的菜单)2-如果会话超时返回我的登录视图

so if i click on the link and session is timeout , the log on view open in the div not in the whole page

所以,如果我点击链接并且会话超时,则登录视图在div中打开而不是在整个页面中

what i should do so if the session timeout it return logon view in new page in on my div?

我应该怎么做如果会话超时它返回我的div上的新页面中的登录视图?

2 个解决方案

#1


11  

An efficient way to handle a session expiry is to create a custom Authorization attribute and return a HTTP 403 response if the session has expired and were dealing with an ajax request.

处理会话到期的有效方法是创建自定义Authorization属性,并在会话已过期并且正在处理ajax请求时返回HTTP 403响应。

To create an Ajax aware authorization attribute you can inherit from AuthorizeAttribute and override the HandleUnauthorizedRequest event with a check on the type of request eg. IsAjaxRequest()

要创建一个支持Ajax的授权属性,您可以从AuthorizeAttribute继承并覆盖HandleUnauthorizedRequest事件,并检查请求的类型,例如。 IsAjaxRequest()

public class AjaxAuthorizeAttribute : AuthorizeAttribute
{
    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        if (filterContext.HttpContext.Request.IsAjaxRequest())
        {
            // Fire back an unauthorized response
            filterContext.HttpContext.Response.StatusCode = 403;
        }
        else
            base.HandleUnauthorizedRequest(filterContext);
    }
}

Then just decorate your controllers or actions with the AjaxAuthorize attribute just as you normally would with Authorize

然后就像使用Authorize一样,使用AjaxAuthorize属性装饰控制器或操作

[AjaxAuthorize(Roles = "1,2,3,4,5")]
public class HomeController 
{

Then if you're using jQuery you can handle the 403 response by creating a global ajax error handler.

然后,如果您正在使用jQuery,则可以通过创建全局ajax错误处理程序来处理403响应。

    $.ajaxSetup({
        error: function (x, e) {
            if (x.status == 403) {
                alert("Sorry, your session has expired. Please login again to continue");
                window.location = "/login";
            }
        }
    });

#2


0  

You can redirect user to login page on Session_Start event in Global

您可以在Global中的Session_Start事件上将用户重定向到登录页面

protected void Session_Start()
    {
        GeneRateKey();
        if (Session["Username"] != null)
        {
            //Redirect to Welcome Page if Session is not null  
            HttpContext.Current.Response.Redirect("~/WelcomeScreen", false);

        }
        else
        {
            //Redirect to Login Page if Session is null & Expires                   
            new RedirectToRouteResult(new RouteValueDictionary { { "action", "Index" }, { "controller", "Login" } });
        }
    }

#1


11  

An efficient way to handle a session expiry is to create a custom Authorization attribute and return a HTTP 403 response if the session has expired and were dealing with an ajax request.

处理会话到期的有效方法是创建自定义Authorization属性,并在会话已过期并且正在处理ajax请求时返回HTTP 403响应。

To create an Ajax aware authorization attribute you can inherit from AuthorizeAttribute and override the HandleUnauthorizedRequest event with a check on the type of request eg. IsAjaxRequest()

要创建一个支持Ajax的授权属性,您可以从AuthorizeAttribute继承并覆盖HandleUnauthorizedRequest事件,并检查请求的类型,例如。 IsAjaxRequest()

public class AjaxAuthorizeAttribute : AuthorizeAttribute
{
    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        if (filterContext.HttpContext.Request.IsAjaxRequest())
        {
            // Fire back an unauthorized response
            filterContext.HttpContext.Response.StatusCode = 403;
        }
        else
            base.HandleUnauthorizedRequest(filterContext);
    }
}

Then just decorate your controllers or actions with the AjaxAuthorize attribute just as you normally would with Authorize

然后就像使用Authorize一样,使用AjaxAuthorize属性装饰控制器或操作

[AjaxAuthorize(Roles = "1,2,3,4,5")]
public class HomeController 
{

Then if you're using jQuery you can handle the 403 response by creating a global ajax error handler.

然后,如果您正在使用jQuery,则可以通过创建全局ajax错误处理程序来处理403响应。

    $.ajaxSetup({
        error: function (x, e) {
            if (x.status == 403) {
                alert("Sorry, your session has expired. Please login again to continue");
                window.location = "/login";
            }
        }
    });

#2


0  

You can redirect user to login page on Session_Start event in Global

您可以在Global中的Session_Start事件上将用户重定向到登录页面

protected void Session_Start()
    {
        GeneRateKey();
        if (Session["Username"] != null)
        {
            //Redirect to Welcome Page if Session is not null  
            HttpContext.Current.Response.Redirect("~/WelcomeScreen", false);

        }
        else
        {
            //Redirect to Login Page if Session is null & Expires                   
            new RedirectToRouteResult(new RouteValueDictionary { { "action", "Index" }, { "controller", "Login" } });
        }
    }