views:

498

answers:

3

Hi,

I'm inserting something in the cache when the user does a login.

Now I want to delete that from the cache when the user's session expires. HttpContext is null .. so I don't know for which user the session expired. How can I go about finding this ?

EDIT: unfortunately SessionID doesn't offer me much. What i'm doing in this particular case is using an HttpModule that handles AuthorizeRequest to insert the current userName in Cache to ensure that another user from another machine can't login. But the Session is null in the HttpModule. So I can't use that. Any other suggestions ?

+1  A: 

Handle the Session_End event in your global.asax. Here you should have access to the SessionID.

Rune Grimstad
+1  A: 

You can get to the session in a HttpModule, you just have to wait until it is initialised, ie the AcquireRequestState event (http://msdn.microsoft.com/en-au/system.web.httpapplication.acquirerequeststate.aspx)

buggs
+1  A: 

Assuming that you are accessing the cached item on each page, you could set the cached item to have a sliding expiration. Set the expiration time to the same as your session timeout. With a sliding expiration, the cache will refresh every time the object is accessed and will be disposed of if it is not accessed within that time.

See http://msdn.microsoft.com/en-us/library/4y13wyk9.aspx for more information.

Jason Z