我正在网站上实现登录/注销功能。 每当用户单击“ 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.
更多推荐
发布评论