gpt4 book ai didi

c# - session 超时刷新元标记重置 .NET

转载 作者:行者123 更新时间:2023-11-30 22:56:08 27 4
gpt4 key购买 nike

我想在 session 结束或重定向到 .net Web 应用程序的登录页面时显示一条消息。

我尝试使用 Session_End global.asax 事件,但当此事件触发时,上下文已关闭,因此我无法重定向到任何页面。

我也尝试发送刷新元标记,但即使用户正在页面上导航,它也会刷新页面,我的意思是,刷新标记永远不会更新。

有没有办法做到这一点?你能帮帮我吗?

提前致谢。

最佳答案

您需要 JS 定期轮询服务器(例如每分钟使用 setTimeout)以询问 我是否仍在登录?。如果它得到 false 作为响应,您的 JS 可以做任何您想做的事情(显示消息、重定向到登录页面等)。

服务器端的实现类似于

[HttpGet]
[AllowAnonymous]
[DoNotExtendAuthentication]
public ActionResult IsAuthenticated(string url)
{
var isAuthenticated = HttpContext?.User?.Identity?.IsAuthenticated ?? false;
return Json(isAuthenticated); // change this line to whatever shape of JSON you want
}

public class DoNotExtendAuthenticationAttribute : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
filterContext.HttpContext.Response.Cookies.Remove(
FormsAuthentication.FormsCookieName);
}
}
需要

DoNotExtendAuthenticationAttribute 才能有效禁用表单例份验证的滑动过期逻辑。

对于您的路由,请务必使用 SessionlessHandler这样它就不会持续扩展 Session

关于c# - session 超时刷新元标记重置 .NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54629774/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com