在我的托管环境中,我的会话超时非常短,有时甚至2秒就会超时。
如果用户继续使用网站,则重置会话,除非会话= null且计数为0。
会话应在20分钟后超时,然后将用户重定向到登录页面
代码如下:
protected override void OnInit(EventArgs e)
{
if (this.Session != null && this.Session.Count > 0)
{
string email = (string)this.Session["Email"];
int practiceId = (int)this.Session["PracticeId"];
int practitionerId = (int)this.Session["PractitionerId"];
this.ClientScript.RegisterHiddenField("loggedInUserName", email);
this.ClientScript.RegisterHiddenField("practiceId", practiceId.ToString());
this.ClientScript.RegisterHiddenField("practitionerId", practitionerId.ToString());
}
else
{
this.Session.Abandon();
Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
Response.Redirect("~/Default.aspx");
}
base.OnInit(e);
}有人知道为什么我的会话超时会这么短吗?当我有时使用我的网站时,我可以在没有超时的情况下移动2-5分钟,而在其他10分钟内,我会得到一个超时。会话丢失的原因是什么?有没有避免或测试会话丢失的方法?
提前谢谢。
发布于 2013-09-17 00:37:35
我假设您正在覆盖页面的init函数,但是在每次页面加载时可能放弃会话可能会导致更多的问题,而不是它解决的问题。我将检查母版页中是否存在会话:
protected void Page_Init(object sender, EventArgs e)
{
if (!HttpContext.Current.User.Identity.IsAuthenticated)
{
// user is not logged in
string ReturnUrl = HttpContext.Current.Request.Url.PathAndQuery;
string RedirectUrl = "/Login.aspx";
if (!String.IsNullOrEmpty(ReturnUrl))
{
RedirectUrl += "?ReturnUrl=" + Server.UrlEncode(ReturnUrl);
}
Response.Redirect(RedirectUrl);
}
}如果这是在母版页中,它将检查将用户重定向到登录页的每个请求(向从母版继承的aspx页发出)。
如果您的应用程序正在共享应用程序池,则您可能会与其他应用程序共享cookie id:
<authentication mode="Forms">
<forms loginUrl="~/Login.aspx" timeout="60" name="MY_COOOKIE_NAME" slidingExpiration="true" />
</authentication>
<sessionState timeout="60" />我的COOKIE名称将识别你的应用程序使用的cookie,其他应用程序可能会使用默认的cookie名称,因此您的会话尽管表面上已通过身份验证,但并不属于该应用程序,因为它们会被其他应用程序覆盖。滑动过期意味着您的会话时间将在您每次访问页面时延长。
另外,检查machineKey配置元素是否存在,这使我的会话更稳定:
<machineKey
validationKey="random_validation_key"
decryptionKey="random_decryption_key"
validation="SHA1" decryption="AES" />https://stackoverflow.com/questions/18829641
复制相似问题