如何在会话消失时重定向 [英] How to redirect back when session is gone
问题描述
但当我把直接链接像localhost://welcome.aspx
我向我显示了错误的欢迎页面!
如果没有登录的话,我需要重定向回登录页面用户ID和pw
即使输入登录名和pw正确,我试过的以下代码也不会让我留在欢迎页面上
我的尝试:
if(!isPostback)
{
response.redirect(home.aspx);
}
这样做的方法很少。
1.纠正你的代码。
// if(!isPostback)
if (会话[ MySession]!= null )
{
response.redirect( home.aspx跨度>);
}
请勿将此包装在回发检查中,因为您需要这两个条件,即首次加载和回发时。想一想当你登录到应用程序并闲置几个小时,然后你点击页面中的一个按钮然后你就不会期望你的代码做到这一点,因为会话已经过期了。 />
2.使用Global.aspx
查看以下链接了解更多详情 -
重定向页面后ASP.Net中的会话超时 [ ^ ]
希望,有帮助:)
after login I have a welcome page
but when I put direct link like localhost://welcome.aspx
I shows me the welcome page which is wrong !
I need to redirect back to the page of login if any one who is not logged with user id and pw
the following code which I Have tried won't let me stay on welcome page even if entered login and pw correctly
What I have tried:
if(!isPostback)
{
response.redirect("home.aspx");
}
There are few approaches to do this.
1. Correcting your code.
//if(!isPostback) if(Session["MySession"]!=null) { response.redirect("home.aspx"); }
Do not wrap this in the postback check as you'll be needing this for both the conditions i.e, on first load and postbacks. Think about a scenario when you are logged in to the application and are idle for few hours and then you are clicking one button in the page then you wouldn't be expecting your code to do it's stuff as session will already be expired.
2. usingGlobal.aspx
Check following link for further details-
Redirect Page After Session Time Out in ASP.Net[^]
Hope, it helps :)
这篇关于如何在会话消失时重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!