2
votes

I have a Blazor WebAssembly project, with a Server-side API for authentication. I have added Identity to this Server project and this seems to work. Both registering and logging in works.

But as soon as I add CascadingAuthenticationState tag to my App.razor of my WebAssembly app, the Webassembly app breaks and prints the error: Cannot provide a value for property 'AuthenticationStateProvider' on type 'Microsoft.AspNetCore.Components.Authorization.CascadingAuthenticationState'.

I see a lot of posts about this, and the solution every time seems to be to add builder.Services.AddAuthorizationCore() to the Program.cs file, but this does not fix anything.

Since this is a WebAssembly project that already existed before I added Identity myself, I suspect something is missing. Can anyone think of something I might check?

My Program class:

public static async Task Main(string[] args)
{
   var builder = WebAssemblyHostBuilder.CreateDefault(args);
   builder.RootComponents.Add<App>("#app");

   builder.Services.AddAuthorizationCore();
   builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

   await builder.Build().RunAsync();
}

My App.razor file:

<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true">
    <Found Context="routeData">
        <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
    </Found>
    <NotFound>
        <LayoutView Layout="@typeof(MainLayout)">
            <p>Sorry, there's nothing at this address.</p>
        </LayoutView>
    </NotFound>
</Router>
</CascadingAuthenticationState>
1

1 Answers

1
votes