会话超时在asp [英] Session Time out in asp
问题描述
如何在asp.net中设置会话超时,如果会话超时,页面必须重定向到login.aspx
请参考以下网址,
成功登录后,添加以下代码以在会话中添加用户数据。
HttpContext.Current.Session [ UserID] = data.UserId;
HttpContext.Current.Session [ Email] = data.Email;
在母版页中添加以下代码。
公开 bool checkAuthentication
{
get ;
set ;
}
受保护 void Page_Load( object sender,EventArgs e)
{
if (checkAuthentication)
{
if ((会话[ UserID] + )== )
{
Response.Write( < script type = \text / javascript \> +
window.parent.location =' + ConfigurationManager.AppSettings [ SiteUrl ] + login?loginUrl ='+ window.parent.location +'& mode = session'; +
< / script>);
Response.End();
}
}
}
在要验证的每个子页面中添加以下代码用户会话。
protected void Page_PreInit( object sender,EventArgs e)
{
this .Master.checkAuthentication = true ;
}
希望这可以帮到你。
默认情况下,会话超时将在20分钟内在ASP.NET中过期。要增加超时或到期,您应该在 web.config 文件中更改SessionState的超时属性
< sessionState timeout = 40 />
请注意,如果您使用的是表单身份验证,则表单超时设置会在设置的超时期限后将用户注销,因此您还必须调整此属性:
< 身份验证 mode = 表单 >
< 表格 timeout = 40 / >
< / authentication >
会话超时后将用户重定向到登录页面类似于在某些间隔后方法刷新页面。唯一不同的是计算页面必须重定向的时间。因此,可以使用Session.timeout属性计算时间,该属性将为该会话提供会话超时值。为该值添加一些优雅时间并自动将用户重定向到登录页面。
使用Window.setTimeout方法
在页面中加载事件:
body.Attributes.Add( onLoad, window.setTimeout( window.location.href ='login.aspx' ,&(Session.Timeout * 60 * 1000 )+ 10000 & );)
使用元标记 - 刷新
Response.AppendHeader( Refresh,Convert.ToStrin g((Session.Timeout * 60 )+ 10 )& ; URL = Login.aspx)
这两种方法都会在会话超时+ 10秒后将用户重定向到登录页面。这是在会话超时之后将用户重定向到登录页面而无需用户交互的方法。
在你的web.config中写这个:
< configuration >
< system.web >
< sessionState timeout = 20 > < / sessionState >
< /system.web >
< / configuration >
希望有所帮助:)
How to set session timeout in asp.net and if the session timeout the page has to redirect to login.aspx
Please refer following url,
After successful login add following code to add user data in session.
HttpContext.Current.Session["UserID"] = data.UserId; HttpContext.Current.Session["Email"] = data.Email;
Add following code in master page.
public bool checkAuthentication { get; set; } protected void Page_Load(object sender, EventArgs e) { if (checkAuthentication) { if ((Session["UserID"] + "") == "") { Response.Write("<script type=\"text/javascript\">" + "window.parent.location = '" + ConfigurationManager.AppSettings["SiteUrl"] + "login?loginUrl=' + window.parent.location + '&mode=session';" + "</script>"); Response.End(); } } }
Add following code in each child page where you want to authenticate user session.
protected void Page_PreInit(object sender, EventArgs e) { this.Master.checkAuthentication = true; }
Hope this may help you.
By default, Session timeouts are set to expire in ASP.NET in 20 minutes. To increase the timeout or expiry you should change the timeout attribute for SessionState in the web.config file
<sessionState timeout="40" />
Note that if you are using Forms authentication, the Forms timeout setting will log the user out after the set timeout period so you will also have to adjust this attribute:
<authentication mode="Forms"> <forms timeout="40"/> </authentication>
Redirecting user to login page after session timeout is similar to refreshing the page after certain intervals method. Only thing which will differ is that calculating time after which the page has to be redirected. Hence time can be calculated using Session.timeout property which will give us session timeout value for that session. Add some grace timings to that value and redirect the user to the login page automatically.
Using Window.setTimeout method
In the page Load event:
body.Attributes.Add("onLoad", "window.setTimeout(""window.location.href='login.aspx'""," & (Session.Timeout * 60 * 1000) + 10000 & ");")
Using Meta Tag - Refresh
Response.AppendHeader("Refresh", Convert.ToString((Session.Timeout * 60) + 10) & "; URL=Login.aspx")
Both these methods will redirect the user to login page after session timeout + 10 seconds. This is how you can redirect the user to login page after session timeout without user interaction.
In your web.config write this:
<configuration> <system.web> <sessionState timeout="20"></sessionState> </system.web> </configuration>
hope it helps :)
这篇关于会话超时在asp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!