HttpContext.Current.Session

0 like 0 dislike
4 views
Hello. Not long ago, faced with a strange problem on the website — there is asp.net mvc project, there is single-tone class SiteUser through whom the entire work with services.
if authorization is successful, the current user is stored in the session collection:

  1. static readonly string sessionKey = "SiteUser";
  2.  
  3. public static SiteUser Create()
  4. {
  5. SiteUser.Current = new SiteUser();
  6.  
  7.     return SiteUser.Current;
  8. }
  9.  
  10. public static SiteUser Current
  11. {
  12. get
  13.     {
  14.         if (HttpContext.Current.Session == null || HttpContext.Current.Session[sessionKey] == null)
  15.         {
  16.             throw new SiteUserAutorizationExeption();
  17.         }
  18.  
  19.         return HttpContext.Current.Session[sessionKey] as SiteUser;
  20.     }
  21. set
  22.     {
  23.         if (!HttpContext.Current.Session == null)
  24.         {
  25. HttpContext.Current.Session[sessionKey] = value;
  26.         }
  27.     }
  28. }

It works, but sometimes (sic!) triggered the exception in the second condition, i.e.
  1. HttpContext.Current.Session[sessionKey] == null
— the session is active, the FormsAuthenticationTicket is not expired.
Adds a puzzle the fact that in the collections of the session, also lie, for example, the culture settings that are not reset. It is clear that nowhere in the code is not a forced reset Session[sessionKey].
Maybe someone faced, prompt, what can I try/to look at/fix?

p.s. the session settings in the web.config:
  1.  mode="InProc" timeout="20" />


update:
as suggested in the comments, I too thought it was just dying session, and tried to keep the length of a session and Loginova a change of variables — but SessionEnd himself never showed :-( and if showed, for the timeout that is written in web.config

so. last night I have downloaded symbol tables of source .net framework, set breakpoints on the edit collection in the depths of the HttpContext (SessionStateItemCollection) and waited for that to click... but the bug never manifested itself, I decided to put a timeout for a minute to see what happens with the session and collection — the autopsy showed a few things:
1) NULL accept all the elements of a collection, just culture settings, quickly and deftly put back yourself
2) reset occurs at the end of the session, obviously

in principle, at least it is now clear that deleted not just one element of a collection session, and all session at once. it is still unclear why she allows herself to behave this way, because in the web.config to 20 minutes
by | 4 views

1 Answer

0 like 0 dislike
Yeah, probably not much help, but would advise basic logging. The first is the Session_Start and Session_End methods and encapsulate the write/read/delete values from session and log all actions. I hope someone will offer a more constructive solution.
by

Related questions

0 like 0 dislike
3 answers
asked Mar 24, 2019 by AbnormalUnit
0 like 0 dislike
2 answers
asked Mar 25, 2019 by Illorian
0 like 0 dislike
1 answer
asked Mar 21, 2019 by Francyz
0 like 0 dislike
1 answer
asked Mar 25, 2019 by masterOk
0 like 0 dislike
6 answers
110,608 questions
257,186 answers
0 comments
28,871 users