C# mvc中会话超时的处理

C# mvc中会话超时的处理,c#,ajax,asp.net-core,asp.net-core-mvc,session-timeout,C#,Ajax,Asp.net Core,Asp.net Core Mvc,Session Timeout,我正在尝试在.net核心应用程序中实现会话超时。重定向到登录页面在非ajax请求/完整回发中工作正常,但在ajax请求中则不行。登录页面显示在ajax请求的布局/当前页面中 我已经编写了一个中间件,它将首先调用控制器方法,并在其中写入重定向登录。下面是我的代码 中间件 app.Use(async (ctx, next) => { if (ctx.GetTenantContext<AppTenant>() == null

我正在尝试在.net核心应用程序中实现会话超时。重定向到登录页面在非ajax请求/完整回发中工作正常,但在ajax请求中则不行。登录页面显示在ajax请求的布局/当前页面中

我已经编写了一个中间件,它将首先调用控制器方法,并在其中写入重定向登录。下面是我的代码

中间件

 app.Use(async (ctx, next) =>
            {
                if (ctx.GetTenantContext<AppTenant>() == null && !ctx.Request.Path.ToString().Contains("/Home/Redirect"))
                {
                    string redirect = "/Home/Redirect/";

                    if (ctx.Request.Path.ToString().Contains("Admin"))
                    {
                        redirect = "/Home/Redirect/Admin";
                    }
                    else
                    {
                        redirect = "/Home/Redirect/Trainee";
                    }


                    ctx.Response.Redirect(redirect, true);
                }
                else
                {
                    await next();
                }
            });

在此感谢您的帮助

在这种情况下,服务器会返回ajax请求的重定向响应,但用户不会被重定向到登录页面。为什么?原因是HTTP重定向由浏览器隐式处理,实际上从未到达ajax成功回调。浏览器处理重定向并提供一个200代码,其中包含重定向目的地的内容(在您的案例中是登录页面)

这并不像听起来那么简单,解决办法很少,但所有这些都相当复杂。以下是您可能尝试实施的一个解决方案:

另一个解决方案是让一些javascript代码在每个页面上以特定的间隔运行,以检查会话是否已过期(通过查询服务器,这会使事情更加复杂)。每当此javascript代码检测到会话已过期时,应立即将用户带到登录页面,而不是等待触发ajax请求。
查询服务器的问题是,如果服务器上存在某种身份验证票证的滑动过期,票证可能会被续订,会话可能永远不会过期。

您是说,即使会话超时后发出ajax请求,您也希望用户重定向到登录页面?是的,确实如此,如果会话超时,用户应该重定向到ajax请求中的登录页面。非常感谢您的帮助!它真的帮助了我!
[Route("/Home/Redirect/{AppType?}")]
        public async Task<IActionResult> Redirect()
        {
            string appType = string.Empty;
            string clientName = string.Empty;

            if (!string.IsNullOrEmpty(Convert.ToString(RouteData.Values["AppType"])))
            {
                appType = Convert.ToString(RouteData.Values["AppType"]);
            }

            await _signInManager.SignOutAsync();

            HttpContext.Session.Clear();

            if (!string.IsNullOrEmpty(appType))
            {
                if (appType == "Admin")
                {
                    if (HttpContext.Request.Cookies != null)
                    {
                        if (HttpContext.Request.Cookies["clientnamebe"] != null)
                        {
                            clientName = HttpContext.Request.Cookies["clientnamebe"].ToString();
                        }
                    }
                    return RedirectToRoute(new
                    {
                        controller = "Admin",
                        action = "Login",
                        clientname = clientName

                    });
                }
                else
                {
                    if (HttpContext.Request.Cookies != null)
                    {
                        if (HttpContext.Request.Cookies["clientnamefe"] != null)
                        {
                            clientName = HttpContext.Request.Cookies["clientnamefe"].ToString();
                        }
                    }
                    return RedirectToRoute(new
                    {
                        controller = "Account",
                        action = "Login",
                        clientname = clientName

                    });
                }
            }

            return View();
        }
[Route("Account/Login/{clientname}", Name = ApplicationType.FRONTEND)]
[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true, Duration = 0)]
public async Task<IActionResult> TraineeLogin(string returnUrl)
{
  Return View();
}
 $('#tabstrip a').click(function (e) {
            e.preventDefault();

            var tabID = $(this).attr("href").substr(1);
            localStorage.setItem("ClientCourseTab", '#'+tabID);
            $("#" + tabID).html("");
            var link = '@Url.Action("-1", "Course")';
            link = link.replace("-1", tabID);
            $("#" + tabID).load(link); // here actual request made
            var appendValue = tabID.replace('_FrontEnd', '');
            var appendValue = appendValue.replace('_', '');
            window.location.hash = appendValue;
            $(this).tab('show');
        });