Can I force AuthenticationState to refresh (when claims have changed)?

David Thielen 3,116 Reputation points
2024-04-11T23:07:09.2+00:00

Hi all;

Is there any way to force the AuthenticationState to re-read the database when claims have changed?

I've implemented a ServerAuthenticationStateProvider and it works great for my adding IdentityUser.Enabled. But when I set it to re-read the claims on a call to ExAuthenticationStateProvider.GetAuthenticationStateAsync(), while that does return and updated list of the claims, that list is not propagated to the AuthenticationState .

Is there a way to propigate it other than having the user log out and back in?

My Routes.razor is:

<Router AppAssembly="typeof(Program).Assembly">
    <Found Context="routeData">
        <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
            <NotAuthorized>
                <div class="message--401">
                    <h1 role="alert">Sorry, you're not authorized to view this page.</h1>
                    <p>You must have an Admin claim and be set to use 2FA when logging in.</p>
                </div>
            </NotAuthorized>
        </AuthorizeRouteView>
        <FocusOnNavigate RouteData="@routeData" Selector="h1" />
    </Found>
    <NotFound>
        <PageTitle>Not found</PageTitle>
        <LayoutView Layout="@typeof(MainLayout)">
            <div class="message--404">
                <h1 role="alert">Sorry, there's nothing at this address.</h1>
            </div>
        </LayoutView>
    </NotFound>

thanks - dave

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,577 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 65,131 Reputation points
    2024-04-13T01:33:52.0766667+00:00

    GetAuthenticationStateAsync() is only called once to get the state for cascade. You calling it again will not effect the cascade value. You use NotifyAuthenticationStateChanged(newAuthState) to notify state change listeners of a state change.

    You should add a Reload method to your provider (called from a timer, of whatever triggers a state reload) this should call NotifyAuthenticationStateChanged(newAuthState) if it wants to cascade the update.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.