Google Chrome 的 ASP.NET MVC Session.IsNewSession 问题
我目前正在为 ASP.NET 3.5 MVC 2 项目开发会话过期逻辑,以注销用户并将其重定向到 AccountController LogOn 操作。
我的所有关心会话状态的操作都有以下属性,并且这段代码在 IE 8 中有效,但在 Firefox 4 或 Google Chrome 10 中无效。症状是当我尝试导航到由操作表示的视图时在我的 [SessionExpireFilter] 属性中,以下代码中的 ctx.Session.IsNewSession 属性每次都评估为“true”,即使我只进入 30 分钟会话几秒钟。
public class SessionExpireFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpContext ctx = HttpContext.Current;
// check if session is supported
if (ctx.Session != null && ctx.Session.IsNewSession)
{
// If it says it is a new session, but an existing cookie exists, then it must
// have timed out
string sessionCookie = ctx.Request.Headers["Cookie"];
if ((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0))
{
FormsAuthentication.SignOut();
ctx.Response.Redirect("~/Account/LogOn");
}
}
base.OnActionExecuting(filterContext);
}
}
有什么方法可以弄清楚为什么 Chrome 和 Firefox 会出现这种情况,而 IE 却不会?提前致谢。
编辑:正如我最初认为的那样,这在 FF 中不起作用。登录并尝试使用 SessionExpireFilter 属性访问操作后,我立即被定向到 LogOn 操作。
I'm currently working on a Session expired piece of logic for my ASP.NET 3.5 MVC 2 project to log out a user and redirect them to the AccountController LogOn action.
I have the following attribute on all my actions that care about session state, and this piece of code works in IE 8, but not Firefox 4 or Google Chrome 10. The symptom is when I attempt to navigate to a view represented by an action with my [SessionExpireFilter] attribute, the ctx.Session.IsNewSession property in the below code is evaluating as "true" every time, even if I'm only seconds into my 30-minute session.
public class SessionExpireFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpContext ctx = HttpContext.Current;
// check if session is supported
if (ctx.Session != null && ctx.Session.IsNewSession)
{
// If it says it is a new session, but an existing cookie exists, then it must
// have timed out
string sessionCookie = ctx.Request.Headers["Cookie"];
if ((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0))
{
FormsAuthentication.SignOut();
ctx.Response.Redirect("~/Account/LogOn");
}
}
base.OnActionExecuting(filterContext);
}
}
Is there any way to figure out why Chrome and Firefox are behaving this way, but IE is not? Thanks in advance.
EDIT: This is not working in FF as I originally believed. I am being directed to my LogOn action immediately after logging in and attempting to access an action with my SessionExpireFilter attribute.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ASP.NET 将为每个请求创建一个新会话,除非您在其中存储了某些内容。
尝试将以下代码添加到您的
Global.asax
中。它可以在我的 MVC2 和 MVC3 应用程序中使用相同的SessionExpireFilterAttribute
运行。ASP.NET will create a new session for every request unless you store something in it.
Try adding the code below to your
Global.asax
. It works in my MVC2 and MVC3 apps with the sameSessionExpireFilterAttribute
.我们可以在 MVC 应用程序的 Golbal.asax 文件中添加 session_start 方法。
然后,当应用程序启动时,将创建您的会话。然后会话将不是 isNewSession 'True',否则它将始终'True'
We can add session_start method in Golbal.asax file in MVC appliaction.
Then when application starting your session will be created. and then session will be not isNewSession 'True', otherwise it will be always 'True'