sessionblazorserver-application

How to clear all the sessions using ProtectedSessionStorage in blazor server app?


I am utilizing a Blazor server application in which I have utilized the ProtectedSessionStorage key pair method to store sessions. However, I am facing an issue when it comes to deleting or clearing all the sessions I have stored upon logging out of the application. While there is a way to delete sessions by key, I am seeking a solution to delete all sessions simultaneously.

I have utilized the following code to establish and retrieve sessions, and to date, I have saved a total of 25 sessions. Here is an example of one of the values. However, I am currently attempting to delete these 25 stored values, but I am unsure of how to achieve this.

//set session ProtectedSessionStore.SetAsync("Client", null);

//get session var clientID = await ProtectedSessionStore.GetAsync("Client");

//Delete multiple sessions ???????


Solution

  • Since ProtectedSessionStorage lacks the ClearAll() feature, consider invoking a JavaScript method to clear all keys from either localStorage or sessionStorage upon user logout as an alternative.

    @inject IJSRuntime jsRuntime 
    
    @code {
        public async ValueTask RemoveAllStorageKeys()
        {
            await jsRuntime.InvokeVoidAsync("removeAllStorageKeys");
        }
    }
    

    JS:

    <script>
        window.removeAllStorageKeys = () => {
            localStorage.clear(); //remove all keys from localStorage 
            //sessionStorage.clear(); //remove all keys from sessionStorage 
        }
    <script>