0
votes

I've implemented a custom AuthenticationStateProvider. For some reason, I cannot debug this method below, which should run whenever you boot up the app. This is causing me not to know why my token is not being retrieved.

I'm fairly certain this method is being called, because the Task.Delay does delay the app from appearing.

        public override async Task<AuthenticationState> GetAuthenticationStateAsync()
        {
        //await Task.Delay(90000000);

        //Runs everytime the app reloads/initial load.
        var savedToken = await _localStorage.GetItemAsync<TokenResponse>("TokenVal");

        if (string.IsNullOrWhiteSpace(savedToken?.access_token))
        {
            return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
        }
        
        if (!await ValidateCurrentToken(savedToken.access_token))
        {
            return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
        }

        return await MarkUserAsAuthenticated(savedToken.access_token);
    }

Registered it in Program.cs

        builder.Services.AddAuthorizationCore();
        builder.Services.AddBlazoredLocalStorage();
        builder.Services.AddScoped<AuthenticationStateProvider, TVAuthenticationStateProvider>();

I can login, takes me into the app ,authenticated. I refresh and it takes me back to login, and this method is never hit, which would grab the token, validate it and not make me login again. I've got this working fine on a Blazor Server side app.

Here is my App.razor too, I've got CascadingAuthenticationState setup..

<CascadingAuthenticationState>
    <Router AppAssembly="@typeof(Program).Assembly">
        <Found Context="routeData">
            <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
                <NotAuthorized>
                    <Item.Shared.Utilities.LoginRedirect></Item.Shared.Utilities.LoginRedirect>
                </NotAuthorized>
                <Authorizing>
                    Hello?
                    <Chase Center="true" Color="#235273" Size="150px" style="margin-top: 10%;" />
                </Authorizing>
            </AuthorizeRouteView>
        </Found>
        <NotFound>
            <LayoutView Layout="@typeof(MainLayout)">
                <p>Sorry, there's nothing at this address..</p>
            </LayoutView>
        </NotFound>
    </Router> </CascadingAuthenticationState>
1
You should clarify what question you are asking - it's not obvious - is this question about debugging or about auth? - Mister Magoo
@MisterMagoo Thanks, I've edited it to try and clarify. Basically I can't debug that method, even though like fairly certain it is being called at startup. - Dylan

1 Answers

0
votes

Try adding await Task.Delay(5000); as the first line of code in Main - it gives the debugger time to attach before any other code runs.