如何防止ASP.NET中的页面到期 [英] How do I prevent to page expiry in ASP.NET

查看:65
本文介绍了如何防止ASP.NET中的页面到期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的网络应用程序使用asp.net和sql server。

我的文件发布在共享的Windows托管服务器上,默认会话长度被网络托管服务覆盖了5分钟。 />
为此我决定使用cookie代替会话。



现在问题是...如果我在3分钟后使用页面它会去到名为sessionexpire.html的自定义错误页面.....我无法理解问题...实际上发生了什么......请帮助我....



我尝试过:



i am using asp.net and sql server for my web application.
my files are published in a shared windows hosting server,the default session length overridden by web hosting service 5 mins.
for that i decide to use cookie instead of session.

now the problem is...if i use a page after 3 mins its going to the custom error page named "sessionexpire.html".....i can not understand the problem...actually what is going on...please help me....

What I have tried:

private void CheckCookie()
{
HttpCookie CookieName = Request.Cookies["MyCookie"];
CookieName.Expires = DateTime.Now.AddMinutes(30);
if (CookieName != null)
{
Uid = Request.Cookies["MyCookie"]["UserId"];
UTyp = Request.Cookies["MyCookie"]["UserType"];
UName = Request.Cookies["MyCookie"]["name"];
ArrangerId = Request.Cookies["MyCookie"]["ArrangerId"];
}
}







protected void Page_Load(object sender, EventArgs e)
{
try
{
CheckCookie();
}
catch (Exception ex)
{
string s = ex.Message;
Response.Redirect("logout");
}
}







和网络配置我写的



sessionState mode =InProctimeout =30

编译debug =true和targetFramework =4.0

customErrors mode =On

defaultRedirect =sessionexpire.html

身份验证模式=表格

和表格超时= 30




and in web config i have written

sessionState mode="InProc" timeout="30"
compilation debug="true" and targetFramework="4.0"
customErrors mode="On"
defaultRedirect="sessionexpire.html"
authentication mode="Forms"
and forms timeout="30"

推荐答案

检查您的时间:记住cookie是基于客户端的,因此任何到期时间都将相对于客户端上的本地时间,而您是设置相对于服务器上本地时间的到期时间。如果它们相差几分钟就可能导致到期。或者更可能的是,服务器完全处于不同的时区,并且您选择的到期时间已经过了客户端!



所以首先看一下cookie - 使用Chrome,您可以在设置中轻松检查cookie,其他浏览器也应该相当简单 - 或者在设置cookie时将页面的到期时间写入,并在浏览器窗口中查看手册比较。



如果您的托管服务覆盖默认会话超时,则不要在您的配置中添加覆盖代码 - 它会给出会话长度的误导性印象当你忘记为什么它实际上不起作用时,它可能会回来咬你...:笑:
Check your times: remember that the cookie is client based, so any expiration time will be relative to the local time on the client, while you are setting the expiration time relative to the local time on the server. If they are different by a couple of minutes that could cause the expiry. Or as is more likely, the server is in a different timezone completely, and the expiry time you select has already passed for the client!

So start by looking at the cookie - use Chrome and you can inspect cookies quite easily in "Settings" and the other browsers should be fairly simple as well - or write the expiry time to your page when you set the cookie and look at it in the browser window for a manual comparison.

And if your hosting service is overriding the default session timeout, then don't add override code to your configuration - it give the misleading impression of the session length which will probably come back to bite you later when you have forgotten why it doesn't actually work... :laugh:


这篇关于如何防止ASP.NET中的页面到期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆