Session.Clear ?

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

When I log of I do:

HttpContext.Current.Session.Clear()
FormsAuthentication.SignOut()


The problem is that it clears the variables that I set up in my
Session_Start function in my Global.asax file. One of the variables I set
up is:

Session("Start") = DateTime.Now.ToString

This allows me to track the time that the use is in our system. The
Session.Clear() gets rid of it.

Is there a way to do a good way to do a Clear, but have it not touch some of
the variables?

Thanks,

Tom
 
There is no method that comes with it that can do this. That's pretty
specific.

Why not write your own ClearSession method, that can delete everything one
by one, provided it is not one of the special session items that you want to
keep.
 
Clint said:
Loop through Session.Keys maybe.


Clint Hill
H3O Software
http://www.h3osoftware.com
This is exactly the solution.
Here is the code:
(This example is for cache but it works the same in Session)

IDictionaryEnumerator CacheEnum = Cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
Cache.Remove(CacheEnum.Key.ToString());
//cacheList.Add(CacheEnum.Key,CacheEnum.Value);
}
 
Since you already know which Session Items have been set,
why don't you *only* remove the ones you want to get rid of,
with Session.Contents.Remove, instead of using Session.Clear() ?

Session.Contents.Remove("ThisSessionVariableIsGone")
....but the others remain.



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
Juan T. Llibre said:
Since you already know which Session Items have been set,
why don't you *only* remove the ones you want to get rid of,
with Session.Contents.Remove, instead of using Session.Clear() ?

Session.Contents.Remove("ThisSessionVariableIsGone")
...but the others remain.

The other thought I had was to save the ones I am interested in keeping, do
the clear and then put them back.

I could loop through, but there could be 30 or more and then I would need to
check each one to see if it was one I wanted to keep. But since I would
obviously need to know which I wanted to keep, why not just save the ones I
want temporarily and then put them back.

Something like:

Dim OldStart = Session("start") 'Keep this as the clear will dump it
HttpContext.Current.Session.Clear()
session("Start") = oldStart 'Put the start time back.



I would think this would be more efficient than 30 or more compares (in the
loop) and then 30 or more removes (less the ones I want to keep). Of
course, I might be wrong here.

Thanks,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top