Blazor WebAssembly:无法为属性';AuthenticationStateProvider';

Blazor WebAssembly:无法为属性';AuthenticationStateProvider';,blazor,blazor-webassembly,Blazor,Blazor Webassembly,我有一个Blazor WebAssembly项目,带有用于身份验证的服务器端API。我已经将身份添加到这个服务器项目中,这似乎是可行的。注册和登录都有效 但是,一旦我将CascadingAuthenticationState标记添加到我的WebAssembly应用程序的my App.razor,WebAssembly应用程序就会中断并打印错误:无法为类型为“Microsoft.AspNetCore.Components.Authorization.CascadingAuthenticationS

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

但是,一旦我将
CascadingAuthenticationState
标记添加到我的WebAssembly应用程序的my App.razor,WebAssembly应用程序就会中断并打印错误:
无法为类型为“Microsoft.AspNetCore.Components.Authorization.CascadingAuthenticationState”的属性“AuthenticationStateProvider”提供值。

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

因为这是一个WebAssembly项目,在我自己添加Identity之前就已经存在了,所以我怀疑缺少了什么。 谁能想到我可以查一下吗

我的
程序
课程:

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();
}
公共静态异步任务主(字符串[]args)
{
var builder=WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add(“#app”);
builder.Services.AddAuthorizationCore();
addScope(sp=>newHttpClient{BaseAddress=newURI(builder.HostenEnvironment.BaseAddress)});
等待builder.Build().RunAsync();
}
我的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>

对不起,这个地址什么也没有


您需要将此行添加到您的主菜单中:

builder.Services.AddApiAuthorization();
有关详细信息,请参阅: