asp.net会话超时重定向到主页
问题描述:
我有web应用程序和网页上的会话超时和用户交互,这需要重定向到主页/登录页面上的净asp.net会话超时重定向到主页
发现
解决方案1)会话检查应用程序的所有aspx页面的page_load。 2)在Global.asax中的会话起始码
public void Session_Start
{
Response.Redirect("home.aspx");
// or Server.Transfer("home.aspx");
}
我打算第二个选项,让我知道 1)无论我是在正确的方式或本没有更好的办法? 2)在第二个选项是否使用Response.Redirect或Server.Transfer的
-Thanks
答
你为什么不使用JavaScript来做到这一点?你可以使用setTimeout
方法类似
<script type="text/javascript">
setTimeout('window.location = "home.aspx"', 3000);
</script>
把上面的js代码块到页标题这3000是你的会话超时。
答
我会去的第一个,做检查的会议.....
写在母版页的OnInit方法下面的代码会做你的大头钉容易
/// <summary>
/// Check for the session time out
/// </summary>
/// <param name="e"></param>
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (Context.Session != null)
{
//check whether a new session was generated
if (Session.IsNewSession)
{
//check whether a cookies had already been associated with this request
HttpCookie sessionCookie = Request.Cookies["ASP.NET_SessionId"];
if (sessionCookie != null)
{
string sessionValue = sessionCookie.Value;
if (!string.IsNullOrEmpty(sessionValue))
{
// we have session timeout condition!
Response.Redirect("Home.aps");
}
}
}
}
}
可以请你让我知道与第二种解决方案相比的好处(我已经提到)。第二,global.asax文件中只有一行代码。 – Yogesh 2011-04-09 14:26:03