I’ve chat feature in my website and I’m using Ajax post after every 10sec to call WebMethod to refresh list of online users, thats why session doesn’t timeout due to Ajax post after every 10sec. How should i handle session timeout with ajax post?
<sessionState mode="InProc" timeout="15"/>
<authentication mode="Forms">
<forms name="PakistanLawyersLogin" loginUrl="Login.aspx"
timeout="14" slidingExpiration="false"/>
</authentication>
This is WebMethod which is called after every 10sec to get list of online users.
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string getContactList(object userID) {
string respuesta = "";
try {
int _userID = Convert.ToInt16(userID);
DCDataContext dc = new DCDataContext();
DateTime allowTime = DateTime.Now.AddMinutes(-1);
//DateTime allowTime = DateTime.Now.AddDays(-5); //esto lo uso para hacer pruebas
var onlineUsers = from d in dc.Chat_usuarios where d.lastPop > allowTime && d.id != _userID select d;
JObject Opacientes = new JObject(
new JProperty("onlineUsers",
new JObject(
new JProperty("count", onlineUsers.Count()),
new JProperty("items",
new JArray(
from p in onlineUsers
orderby p.userName
select new JObject(
new JProperty("id", p.id),
new JProperty("userName", p.userName.Trim())
))))));
respuesta= Opacientes.ToString();
}
catch { respuesta = "error"; }
return respuesta;
}
If I’m understanding correctly, you want the user’s session to timeout due to inactivity, but the constant polling keeps the session alive. Do you know what the criteria you want to use to determine that a user is inactive?
One thing you could do is to store a “LastUserInput” DateTime as a separate Session Variable. Evert time the user input’s data into chat, update this variable. Then, on each request, get a TimeSpan by comparing the DateTime.Now – Session[“LastUserInput”] and if the elapsed time is >= whatever you’d like the TimeOut to be, you can programatically kill their session.
Updated to provide code example