在Response.Redirect之后检测会话超时?(Detect session timeout after Response.Redirect?)

编程入门 行业动态 更新时间:2024-10-23 17:33:41
在Response.Redirect之后检测会话超时?(Detect session timeout after Response.Redirect?)

我正在网站上实现登录/注销功能。 每当用户单击“ Sign out按钮(在登录页面的任何位置, Login.aspx )时,将执行以下方法:

protected void SignOut(object sender, EventArgs e) { Session.Abandon(); Response.Redirect("Login.aspx"); }

现在,当重定向发生时,我想在Login.aspx执行以下操作:

protected void Page_Init(object sender, EventArgs e) { if ( Session_has_timed_out ... ) SessionTimeOutDIV.Text = "Session timed out. Please log in again."; else { // normal logic here ... } }

问:我如何检查会话先前已被终止,因为我需要在实际调用Session.Abandon()之后检查这个(1 Session.Abandon()和(2)我从调用Session.Abandon()位置重定向后Session.Abandon()发生了什么?

I am implementing the log-in/log-out functionality on a website. Whenever the user clicks on Sign out button (anywhere but at the login page, Login.aspx), the following method will execute:

protected void SignOut(object sender, EventArgs e) { Session.Abandon(); Response.Redirect("Login.aspx"); }

Now, when the redirect happens, I want to do the following in Login.aspx:

protected void Page_Init(object sender, EventArgs e) { if ( Session_has_timed_out ... ) SessionTimeOutDIV.Text = "Session timed out. Please log in again."; else { // normal logic here ... } }

Q: How do I check that the session was previously terminated, given that I need to check this (1) after the actual call to Session.Abandon() and (2) after I am redirected from where the call to Session.Abandon() had happened?

最满意答案

是的,这就是为什么要进行身份验证,建议依赖cookie并使用formsAuth或现在的ASP.Net Identity。 如果它是MVC我们有TempData但是对于webforms我不认为有任何这样的事情。 因此,您可以使用其他状态管理技术,如查询字符串

Response.Redirect("Login.aspx?Logout=true");

在Login.aspx的page_init中

if (Request.QueryString["LogOut"] != null && Request.QueryString["LogOut"] == "true") //do handle exception and casting { //SessionTimeOutDIV.Text = "Session timed out. Please log in again."; } else { // normal logic here ... }

这只是一个黑客,希望这有帮助。

Yes that's why for authentication it is recommended to rely on cookie and use formsAuth or now ASP.Net Identity. If it was MVC we have TempData but for webforms I don't think there is any such thing. So you can use other state management techniques like query string

Response.Redirect("Login.aspx?Logout=true");

In page_init of Login.aspx

if (Request.QueryString["LogOut"] != null && Request.QueryString["LogOut"] == "true") //do handle exception and casting { //SessionTimeOutDIV.Text = "Session timed out. Please log in again."; } else { // normal logic here ... }

This is only a hack, hope this helps.

更多推荐

本文发布于:2023-08-06 08:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446683.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Redirect   Response   Detect   timeout   session

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!