【问题标题】:Blazor WebAssembly: Cannot provide a value for property 'AuthenticationStateProvider'Blazor WebAssembly:无法为属性“AuthenticationStateProvider”提供值
【发布时间】:2021-04-07 07:22:27
【问题描述】:

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

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

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

由于这是一个在我自己添加 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();
}

我的 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>

【问题讨论】:

    标签: blazor blazor-webassembly


    【解决方案1】:

    您需要将此行添加到您的主目录:

    builder.Services.AddApiAuthorization();
    

    有关详细信息,请参阅:

    https://docs.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/hosted-with-identity-server?view=aspnetcore-3.1&tabs=visual-studio

    【讨论】:

      猜你喜欢
      • 2021-03-10
      • 2021-08-06
      • 2020-04-18
      • 2022-12-15
      • 2020-09-16
      • 2020-09-15
      • 1970-01-01
      • 2023-03-11
      • 2022-08-02
      相关资源
      最近更新 更多