michaelmarcuccio
michaelmarcuccio

Reputation: 51

Blazor WASM IdentityServer4 Logout Alert

I am using the new Standalone Blazor WASM Authentication flow with IdentityServer4. I would like to provide a message to the user that they were logged out due to inactivity. I already have this functioning with a low quality js alert() but I was wondering if I can make this work with a custom popup window or maybe a redirect parameter sent to identityserver to show them the alert on the identityserver login page.

I can't quite figure out a way to interrupt the immediate redirect that occurs after OnLogoutSucceeded. The js alert() pauses the redirect and works. Could I maybe modify the outgoing login redirect uri to give a parameter to IDS4?

<RemoteAuthenticatorView Action="@Action" OnLogOutSucceeded="LogoutSucceeded">
</RemoteAuthenticatorView>

@code{
    [Parameter] public string Action { get; set; }

    private async Task LogoutSucceeded()
    {
        await JsInterop.InvokeVoidAsync("alert", "You have been logged out due to inactivity.");
    }
}

Upvotes: 1

Views: 651

Answers (1)

michaelmarcuccio
michaelmarcuccio

Reputation: 51

I figured it out:

//program.cs
  builder.Services.AddOidcAuthentication<ApplicationAuthenticationState>(options =>
            {
                //options
            });

//Authentication.razor
<RemoteAuthenticatorViewCore Action="@Action"
                             TAuthenticationState="ApplicationAuthenticationState"
                             OnLogOutSucceeded="LogoutSucceeded"
                             AuthenticationState="AuthState" />

 [Parameter]
    public string Action { get; set; }

    public ApplicationAuthenticationState AuthState { get; set; } = new ApplicationAuthenticationState();

    public bool Idled { get; set; }

    protected override void OnInitialized()
    {
        if (RemoteAuthenticationActions.IsAction(RemoteAuthenticationActions.LogOut, Action))
        {
            var uri = NavManager.ToAbsoluteUri(NavManager.Uri);
            if (QueryHelpers.ParseQuery(uri.Query).TryGetValue("idle", out var param))
            {
                AuthState.Idle = !string.IsNullOrWhiteSpace(param);
            }
        }
    }

  private void LogoutSucceeded(ApplicationAuthenticationState state)
    {
        Idled = state.Idle;
        if (Idled)
        {
             // save redirect for later
            var returnUrl = state.ReturnUrl;
            // cancel redirect
            state.ReturnUrl = null;

              // implement custom flow
        }
    }

Upvotes: 1

Related Questions