Click here to Skip to main content
16,021,169 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to clear a session after logout using a defualt asp.net login wizard?
Posted

C#
Session.Clear();
 
Share this answer
 
Comments
bbirajdar 9-May-13 13:20pm    
shortest and correct answer ..+5
Thanks7872 10-May-13 0:03am    
↑voted.
Renju Vinod 10-May-13 0:31am    
+5
crazie.coder 14-May-13 8:51am    
It is correct answer but can't we use session.abandon() here??
In addition to the above solution, you should use

C#
Session.Abandon()

on the Page_Load event of logout page. Read more here http://msdn.microsoft.com/en-us/library/ms524310(v=vs.90).aspx[^]
 
Share this answer
 
Comments
Thanks7872 10-May-13 0:03am    
↑voted.Its the smarter way to flush the session.
Renju Vinod 10-May-13 0:31am    
+5
These three methods are used to clear session variables from the server.

***Session.Abandon includes Session.RemoveAll and also deletes the session id created for the session. So, subsequent request to .aspx page would create a new session with a new session id.

***Session.RemoveAll will remove all the user defined session variables but still keeps the session active.so if u want to end session you must use Session.Abandon().

***Session.Clear() do the same job as Session.RemoveAll().
 
Share this answer
 
You can refer to wonderful explanation on below link:

What is the difference between Session.Abandon() and Session.Clear()

It states that:

Clear - Removes all keys and values from the session-state collection.

Abandon - removes all the objects stored in a Session. If you do not call the Abandon method explicitly, the server removes these objects and destroys the session when the session times out.
It also raises events like Session_End.

Session.Clear can be compared to removing all books from the shelf, while Session.Abandon is more like throwing away the whole shelf.

Both the answers above are correct,its totally up to you how you want your session to be handled.
 
Share this answer
 
v3
Comments
Renju Vinod 10-May-13 0:30am    
+5 Nice explanation
Thanks7872 10-May-13 0:31am    
Thanks alot Vinod..!
AshishChaudha 10-May-13 1:01am    
My +5 for good explanation..
Thanks7872 10-May-13 1:02am    
Thank you Ashish.
crazie.coder 15-May-13 1:23am    
good one. +5
just use this code
C#
session.clear();
session.abandon();

iot will fix your problem
 
Share this answer
 
v2
Session.Clear();
session.abandon();
or
session["User"] = null;
 
Share this answer
 
There are three Ways:

1) session.clear();
2) session.abandon();
3) session["sessionname"] = null; e.g: session["UserID"] = null;
 
Share this answer
 
v2
on Login_Page write following code on page load

C#
protected void Page_Load(object sender, EventArgs e)
        {
             Session.Remove("ABC");
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900