如何随着会话过期或放弃而将用户重定向到特定页面 [英] How to redirect user to specific page as Session Expire or abandon

查看:132
本文介绍了如何随着会话过期或放弃而将用户重定向到特定页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只创建一个带有会话对象的登录页面.在会话中存储一些值(userId,userName,UserRoleId).据我所知,默认的会话时间为20分钟.我希望如果用户正在观看页面,并且随着会话到期发生,则将其重定向到登录页面.当特定会话期满时,怎么可能会自动触发 的事件?这样我就可以在那里写逻辑了.
我搜索global.asax,然后使用了它,但它不起作用

  void  Session_End(对象发​​送者,EventArgs e)
  {
      Response.Redirect(" );
      // 在会话结束时运行的代码.
      // 注意:仅在会话状态模式下,才会引发Session_End事件
      // 在Web.config文件中设置为InProc.如果会话模式设置为StateServer 
      // 或SQLServer,不会引发该事件.

  }

解决方案

您不能可靠地做到这一点.您只能重定向以响应请求,并且session_end独立于浏览器请求运行时,您不能使用它来重定向客户端.基本上,您不能仅通过页面请求来强制浏览器通过服务器事件导航到页面.

为此,您将需要使用JavaScript计时器,而且这也不可靠,特别是如果用户同时打开两个浏览器选项卡,其中一个使会话继续进行而另一个则处于空闲状态,则尤其如此.如果会话由于超时以外的原因而过期,则会引发此问题,您最好也不要尝试这样做.您可以告诉一个会话何时是一个新会话,然后将其带到页面以告诉他们其旧会话已过期,但这是您可以做的最好的事情.我不会打扰的.


一些文章可以帮助您-
用户提示:会话期满后重定向用户 [在ASP.NET中实现会话超时页面 [ ^ ]
>检测会话超时并重定向到Homepage [ ^ ]

I just make a Login page with session object. Storing some values in session (userId, userName, UserRoleId). As I know that default time of session is 20 mins. I want that if user watching a page and as session expiry happen then redirect him to login page. How it possible is there any event exists which fire automatically as specific session expire? so that i write logic there.
I search about global.asax then i use this but it is not working

void Session_End(object sender, EventArgs e)
  {
      Response.Redirect("login.aspx");
      // Code that runs when a session ends.
      // Note: The Session_End event is raised only when the sessionstate mode
      // is set to InProc in the Web.config file. If session mode is set to StateServer
      // or SQLServer, the event is not raised.

  }

解决方案

You can''t do this reliably. You can only redirect in response to a request and as session_end runs independently of a browser request you can''t use it to redirect the client. Basically you can''t force the browser to navigate to a page via a server-event, only from a page request.

To do this at all you''ll need to use a javascript timer and that is unreliable too, especially if the user has two browser tabs open at once with one keeping the session going and the other idle. Throw into this issues where the session expires for reasons other than a timeout, and you might as well just not bother trying to do this. You can tell when a session is a new session and take them to a page to tell them their old session expired but that''s about the best you can do. I just wouldn''t bother.


Some articles to help you -
User Tips: Redirecting the User when the Session Expires [^]
Implementing a Session Timeout Page in ASP.NET[^]
Detecting Session Timeout and Redirect to HomePage[^]


这篇关于如何随着会话过期或放弃而将用户重定向到特定页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆