How to reproduce a lost Session in asp.net/c#? -
my problem session gets lost randomly, whereas works.
now question is, if somehow possible reproduce session (my session current logged in user). thought maybe need change in master page i'm right checking if session failed, else?
code:
protected void page_load(object sender, eventargs e) { if (session["userid"] == null) { response.clearcontent(); response.write("not agine"); response.end(); } else { response.write(session["userid"].tostring()); } } global.asax
void session_start(object sender, eventargs e) { // code runs when new session started if (httpcontext.current.user != null && httpcontext.current.user htuser) { htuser user = (htuser)httpcontext.current.user; session["userid"] = user.userid; if (user.htdepartments.any() && user.htdepartments.singleordefault().htbusinessunit != null) { int businessunitid = user.htdepartments.first().htbusinessunit.businessunitid; session["businessunnitid"] = businessunitid; } here can see code session
if need more let me know!
thanks , fast answer!
if i'm understand right, user loosing session when it's expired (timed out).
to prevent can store additional keys in cookies (for i.e. user_id , unique_hash per user), write method in global.asax file fire every page load, , check if session expired. if so, method restore session using cookies keys checking them against db.
example code in global.asax:
protected void application_prerequesthandlerexecute(object sender, eventargs e) { if (httpcontext.current.session != null) { // restore session if session lost cookie not // httpcontext.current.session["user_id"] == null && if (httpcontext.current.request.cookies["hash"] != null) { // job here restoresessionmethod(); } } }
Comments
Post a Comment