Asp.net ajax post的会话超时问题
我在我的网站上有聊天功能,我每10秒使用一次Ajax post调用WebMethod来刷新在线用户列表,这就是为什么会话不会因为每10秒使用一次Ajax post而超时。我应该如何使用ajax post处理会话超时Asp.net ajax post的会话超时问题,asp.net,session-timeout,webmethod,Asp.net,Session Timeout,Webmethod,我在我的网站上有聊天功能,我每10秒使用一次Ajax post调用WebMethod来刷新在线用户列表,这就是为什么会话不会因为每10秒使用一次Ajax post而超时。我应该如何使用ajax post处理会话超时 <sessionState mode="InProc" timeout="15"/> <authentication mode="Forms"> <forms name="PakistanLawyersLogin" loginUrl="Logi
<sessionState mode="InProc" timeout="15"/>
<authentication mode="Forms">
<forms name="PakistanLawyersLogin" loginUrl="Login.aspx"
timeout="14" slidingExpiration="false"/>
</authentication>
如果我理解正确的话,您希望用户的会话由于不活动而超时,但是持续轮询使会话保持活动状态。您知道要使用什么标准来确定用户处于非活动状态吗 您可以做的一件事是将“LastUserInput”日期时间存储为单独的会话变量。每次用户将数据输入聊天室时,更新此变量。然后,在每个请求上,通过比较DateTime.Now-Session[“LastUserInput”]获得时间跨度,如果经过的时间>=您希望的超时值,您可以通过编程终止他们的会话 更新以提供代码示例
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string getContactList(object userID)
{
CheckUserInputTimeout();
string respuesta = "";
try
{
//your code
}
catch { respuesta = "error"; }
return respuesta;
}
private void ResetUserInputTimeout()
{
//Call this function from wherever you want to accept user input as a valid indicator that the user is still active
Session["LastUserInput"] = DateTime.Now;
}
private void CheckUserInputTimeout()
{
int iTimeoutInMinutes = 15;
DateTime dtLastUserInput = DateTime.Now;
if (Session["LastUserInput"] != null)
dtLastUserInput = (DateTime)Session["LastUserInput"];
TimeSpan tsElapsedTime = new TimeSpan(DateTime.Now.Ticks - dtLastUserInput.Ticks);
if (tsElapsedTime.TotalMinutes >= iTimeoutInMinutes)
Session.Abaondon();
}
感谢该解决方案,它工作正常,但当用户继续在网站上进行除聊天以外的其他活动时,即使用户在网站上导航,用户的会话也会在15分钟后超时。标准超时应适用于正常请求,这意味着任何地方的活动(包括聊天)都不会导致会话超时。正常超时也会导致聊天超时(应该如此)。这个例子本质上是创建自己的超时计数器,它的标准比标准的更严格。如果愿意,您可以对站点的“不同”部分重复此逻辑(不同的超时跟踪器)。如果你能给我一点关于你正在努力完成什么和你现在正在经历什么的细节,我可以试着进一步帮助你。
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string getContactList(object userID)
{
CheckUserInputTimeout();
string respuesta = "";
try
{
//your code
}
catch { respuesta = "error"; }
return respuesta;
}
private void ResetUserInputTimeout()
{
//Call this function from wherever you want to accept user input as a valid indicator that the user is still active
Session["LastUserInput"] = DateTime.Now;
}
private void CheckUserInputTimeout()
{
int iTimeoutInMinutes = 15;
DateTime dtLastUserInput = DateTime.Now;
if (Session["LastUserInput"] != null)
dtLastUserInput = (DateTime)Session["LastUserInput"];
TimeSpan tsElapsedTime = new TimeSpan(DateTime.Now.Ticks - dtLastUserInput.Ticks);
if (tsElapsedTime.TotalMinutes >= iTimeoutInMinutes)
Session.Abaondon();
}