views:

77

answers:

3

When using the default session functionality of ASP.NET 3.5, is it possible to remove a session by its session id?

Update: I would like to completely remove a specific session, not just a part of it and not just the current user's session.

A: 

are you looking for?

    Session.Remove("sessionName");//Delete an item from the session
    Session.RemoveAll();//Remove all keys and values from the session-state collection
    Session.Abandon();//Cancel the current Session
    Session.Clear();//Remove all keys and values from the session-state collection
Muhammad Akhtar
see update: I would like to completely remove the session.
jao
A: 

Session.Abandon

The Abandon method destroys all the objects stored in a Session object and releases their resources. If you do not call the Abandon method explicitly, the server destroys these objects when the session times out.

When the Abandon method is called, the current Session object is queued for deletion but is not actually deleted until all of the script commands on the current page have been processed. This means that you can access variables stored in the Session object on the same page as the call to the Abandon method but not in any subsequent Web pages.

rahul
is it possible to remove a specific session by calling session.abandon?
jao
@jao: With Session.Abandon() you remove the session of the current user, which is the standard way to do. You do not want your users to be able to remove sessions from other users, do you?
Residuum
In this case that is exactly what I want
jao
I would like to remove a specific session, not just the current user's one.
jao
+1  A: 

I do not think it is possible to remove a specific session, as a user on the website only has access to his / her own session. This session can be removed via Session.Abandon() and friends as Muhammad Akhtar and adamantium pointed out.

Sessions time out, so inactive sessions are removed automatically after the session timeout, a value that can be set by you, e.g. in the web.config. Maybe you are looking for a solution to a problem that does not exist. See MSDN1 or MSDN2.

Residuum