Last post Jul 10, 2013 09:59 PM by Rion Williams
Jul 10, 2013 05:34 PM|godistop1|LINK
Sorry for this low level question, but I am just trying to get clear about sessions. Using Session variables in applications, I would like to reuse the some of the variables name in two different applications. Am I understanding correctly, if used Session.RemoveAll
it would only clear out the session variable that is used in the current application? Example, if user has 2 separate appliations running in browser and have some of same session variables names when the Session.RemoveALL is executed only the variables for
that application would be removed and will not effect the application that is running?
Jul 10, 2013 05:38 PM|Rion Williams|LINK
The Session itself should not be accessible between applications, so each of your applications would have different Session state objects, each with their own set of keys and values that are stored within them.
The Session.RemoveAll() or
Session.Clear() methods will both clear all of the values contained within the Session of the application that they are called within (and will not affect the other application).
Jul 10, 2013 05:41 PM|godistop1|LINK
Thanks so much for quick response and clarification!
Jul 10, 2013 05:41 PM|AidyF|LINK
Whether your browsers share sessions is down to how the browser implements cookies etc. One browser might share sessions between instances, one browser might not.
If your application is having its session shared then clearing in one will clear in the other. If your browser makes your app share sessions across browser instances is nothing your application can control.
Edit: Rion, by "application" I think he means the client browser, not the .net application.
Jul 10, 2013 07:56 PM|Rion Williams|LINK
I believe that is the case.
To clarify, seperate ASP.NET Applications (ie those existing within different application pools) will not be able to share Session values, however if you are referring to "Applications" as actual browser instances then the case could be different.
Browsers are capable of sharing Sessions (as a single ASP.NET Application might server multiple browser instances). However, in this case both of the browsers are using the same Session object so when you call Session.RemoveAll() or Session.Clear()
the values will be removed from both instances (browsers).
If the applications were actually two distinct applications (ASP.NET Applications) then calling those same methods would only clear it out within the Application where the call was made.
Jul 10, 2013 08:05 PM|godistop1|LINK
Yes, I am talking about 2 separate ASP.Net applications. If user opens another tab starts the same application, if I understand correctly, than there could problems of sharing the same session variables. I was researching what would be the best method
of preventing the user from opening another tab and starting the same application that was running. Any suggestions? Your answers have been quite helpful! I am fairly new to ASP.Net and still learning more complicated code for my applications.
Jul 10, 2013 08:22 PM|Rion Williams|LINK
If you are using two seperate ASP.NET Applications (which are completely distinct) they are not going to be able to share Session values between one another (each Application will have its own Session object).
I believe there are a few suggested methods out there for actually storing these values within a database and using that to share between the two applications (as mentioned within
TonyDong's answer in this thread).
You likely wouldn't want to prevent the user from opening a new tab (it's typically best to avoid disabling behaviors that users are typically accustomed to) but if the user opens a new tab and accesses the same application. Since you have two applications,
the browser will likely share the Session if you access the same application within one that is already open.
Consider you have two applications running, ApplicationA and ApplicationB.
If ApplicationA is open in your browser and contains several values within the Session and you opened up a new tab and accessed ApplicationB through it, nothing would really happen involving the Session. A and B woul both have their own Sessions, which would
be completely ignorant of one another.
If you decided to open another tab within your browser of ApplicationA (which is already open), this new tab should be able to access the Session values from your other instance of ApplicationA (your initial window that was opened) and
changes should be persisted between requests.
If you were to clear out all of the Session values from A or B, it would only affect the browser instances of A or B respectively (calling Session.Clear() within one of the browser instances running ApplicationA would affect all of the browser instances
that are running the ApplicationA application).
Jul 10, 2013 08:32 PM|godistop1|LINK
Rion excellent explanation, but the last paragraph I do have a question. You said Session.Clear() would affect all the browser instances, would Session.RemoveAll() act the same way when running the same ApplicationA?
Jul 10, 2013 09:02 PM|Rion Williams|LINK
For all intensive purposes, the RemoveAll() and Clear() methods are identical (RemoveAll() actually calls Clear() internally).
What I was referring to was that the use of Session.Clear() would clear out the Session values of all of the browser instances that were targeting that particular Application. If the values were cleared through calling Session.Clear() within ApplicationA,
all of the browser instances targeting A would lose their Session values, however B and the browser instances targeting B would be unaffected and vice versa.
Jul 10, 2013 09:04 PM|godistop1|LINK
OK, I understand.
Jul 10, 2013 09:59 PM|Rion Williams|LINK
No problem. (Hopefully that explained what I meant by my original answer a bit better)