Open Closed

Clear Session Storage after logout in Blazor #6977


User avatar
0
lan.dang created
  • ABP Framework version: v8.0.0
  • UI Type: Blazor Server
  • Database System: EF Core (SQL Server, Oracle, MySQL, PostgreSQL, etc..)
  • Tiered (for MVC) or Auth Server Separated (for Angular): no
  • Exception message and full stack trace:
  • Steps to reproduce the issue: How can I clear all session storage after logout? ISessionStorage does not allow in LogoutModel as we need to implement it in OnAfterRender

8 Answer(s)
  • User Avatar
    0
    Anjali_Musmade created
    Support Team Support Team Member

    Hello ,

    Please check this solution if it works for you https://stackoverflow.com/questions/77204224/how-to-clear-all-the-sessions-using-protectedsessionstorage-in-blazor-server-app.

    Thank you.

  • User Avatar
    0
    lan.dang created

    It does not work for me, if using sessionStorage.Clear(), I cannot call it from OnGetAsync method in LogoutModel

  • User Avatar
    0
    Anjali_Musmade created
    Support Team Support Team Member

    Hello ,

    Can you please try to add this code in Logout.cshtml.cs file

    Please check once with this code and let me know if anything needed.

    Thanks,

  • User Avatar
    0
    lan.dang created

    As I mentioned, it raises error InvalidOperationException: JavaScript interop calls cannot be issued at this time. This is because the component is being statically rendered. When prerendering is enabled, JavaScript interop calls can only be performed during the OnAfterRenderAsync lifecycle method.

    There is no method OnAfterRenderAsync in LogoutModel to overwrite

  • User Avatar
    0
    lan.dang created

    @Anjali_Musmade do you have any other idea? Thanks

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can try this:

    public partial class Index
    {
        
        [SupplyParameterFromQuery]
        public string Page { get; set; }
        
        [Inject]
        public IJSRuntime JsRuntime { get; set; }
    
        protected async override Task OnInitializedAsync()
        {
            if (Page == "/Account/~/Account/Login")
            {
                await JsRuntime.InvokeVoidAsync("sessionStorage.clear");
            }
        }
    }
    
  • User Avatar
    0
    lan.dang created

    Thank you, this solution seems work for me BTW, I added to OnAfterRenderAsync protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { //Temporary solution to clear session storage if (Page == "/Account/~/Account/Login") { await jsRuntime.InvokeVoidAsync("sessionStorage.clear"); } } await base.OnAfterRenderAsync(firstRender); }

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    It's ok.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 01, 2024, 05:35