Search code examples
javascriptc#.netsession-storage

How to clear javascript SessionStorage in C#


I have a system where I save temporary variables using sessionStorage (javascript).

How can I do to clean them in C # inside a controller?

Tried HttpContext.Session.Clear()

But I was unsuccessful, the variable remains active until I close the browser.


Solution

  • See @Sam Axe's answer.

    HttpContext.Session.Clear() is .NET context (server side). The sessionStorage is JavaScript, so you have to call:

    sessionStorage.clear();

    on the client side, so in the JavaScript code, possibly on some event button click, etc.)

    ref: https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage