Blazor WebAssembly:无法为属性“AuthenticationStateProvider”提供值

Ste*_*ens 4 blazor blazor-webassembly

我有一个 Blazor WebAssembly 项目,带有用于身份验证的服务器端 API。我已将身份添加到该服务器项目中,这似乎有效。注册和登录都有效。

但是,一旦我将CascadingAuthenticationState标签添加到 WebAssembly 应用程序的 App.razor 中,WebAssembly 应用程序就会中断并打印错误: Cannot provide a value for property 'AuthenticationStateProvider' on type 'Microsoft.AspNetCore.Components.Authorization.CascadingAuthenticationState'.

我看到很多关于此的帖子,每次的解决方案似乎都是添加 builder.Services.AddAuthorizationCore()到 Program.cs 文件中,但这并不能解决任何问题。

由于这是一个在我自己添加 Identity 之前就已经存在的 WebAssembly 项目,因此我怀疑缺少某些内容。有人能想到我可以检查的东西吗?

我的Program课:

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();
}
Run Code Online (Sandbox Code Playgroud)

我的 App.razor 文件:

<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>
Run Code Online (Sandbox Code Playgroud)