Session.Abandon();
Session.Clear();
Session["user"]=null

i m trying to clear a Session but these methods are not working..how can i clear this Session plz help me.. thnxx

Dani AI

Generated

Short diagnosis: calling session-termination code and then immediately navigating away can still look like "you are logged in" for three common reasons — the session object is only removed at the end of the request, the browser may show a cached copy of a page, or an authentication cookie re-establishes identity. The posts from and covered removing session data, and rightly pointed at caching; those are part of the picture but not the whole story.

Do this checklist (order matters):

  • If the site uses Forms Authentication, sign the user out and remove the auth cookie before relying on session changes. Example:
    
    System.Web.Security.FormsAuthentication.SignOut();

var authCookie = new HttpCookie(System.Web.Security.FormsAuthentication.FormsCookieName)
{
Expires = DateTime.UtcNow.AddDays(-1)
};
Response.Cookies.Add(authCookie);

- Ensure the session id cookie is expired so the browser does not present the old id on the next request:
```csharp
if (Request.Cookies["ASP.NET_SessionId"] != null)
{
    var c = new HttpCookie("ASP.NET_SessionId") { Expires = DateTime.UtcNow.AddDays(-1) };
    Response.Cookies.Add(c);
}
  • Prevent cached pages from hiding the logout. For pages that show protected content, disable output caching (page directive alternative to setting cache headers):
    <%@ OutputCache Duration="0" Location="None" VaryByParam="None" %>
  • Confirm your session mode in web.config. Out-of-process modes (StateServer/SQLServer) and web-farm setups change how Session_End and abandonment behave.

Quick debugging tips: use browser devtools or Fiddler to inspect cookies after logout, set a server-side breakpoint on Page_Load for the landing page to see whether Session["user"] is actually recreated, and search the project for any code that writes that session key on every request.

Relevant docs: HttpSessionState.Abandon and FormsAuthentication.SignOut are useful references: and FormsAuthentication.SignOut.

Recommended Answers

All 6 Replies

In order to completely erase it from memory: Session.remove("YourSessionName").

In order to clear its data if it's a basic string: session("YourSessionName") = ""

In order to completely erase it from memory: Session.remove("YourSessionName").

In order to clear its data if it's a basic string: session("YourSessionName") = ""

This is also not working

Don't use #2 and #3 line statements. Use Session.Abandon() method.

Don't use #2 and #3 line statements. Use Session.Abandon() method.

this is also not working...

this is my code

protected void Page_Load(object sender, EventArgs e)
    {
   if (Session["user"] != null)
        {
//some codes
            
        }
        else
        {

            Response.Redirect("Default.aspx");
        }
}



protected void Button1_Click2(object sender, EventArgs e)
    {

     
      
        Session.Abandon();
        Response.Redirect("Default.aspx");
    }

try to kill but still it is alive....

I guess you want to disabled the .

Write following line in page_load handler.

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Session.Remove("User");
hope so it works

To remove all Session variable use

Session.Removeall();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.