8

当我使用

@attribute [Authorize]

在 blazor 客户端应用程序的剃须刀页面上,它给了我这个错误

System.InvalidOperationException:无法为类型“Microsoft.AspNetCore.Components.PageDisplay+AuthorizeViewWithSuppliedData”的属性“AuthorizationPolicyProvider”提供值。没有“Microsoft.AspNetCore.Authorization.IAuthorizationPolicyProvider”类型的注册服务

我为客户端设置了身份验证并使用自定义AuthenticationStateProvider,如下所示

 public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddScoped<AuthenticationStateProvider, ApiAuthenticationStateProvider>();
            services.AddSingleton<AuthService, AuthService>();
            services.AddBlazoredLocalStorage();

        }

        public void Configure(IComponentsApplicationBuilder app)
        {
            app.AddComponent<App>("app");
        }
    }

关于这个问题的任何帮助

4

2 回答 2

11

我只需要添加services.AddAuthorizationCore();

感谢 KodiakMx

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddBlazoredLocalStorage();
        services.AddAuthorizationCore();
        services.AddScoped<AuthenticationStateProvider, ApiAuthenticationStateProvider>();
    }
于 2019-09-03T19:03:44.600 回答
3

app.UseAuthentication();您可能需要在课堂上添加Configure方法。Startup

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseCookiePolicy();

    app.UseAuthentication();
    app.UseSession();
    app.UseMvc(routes =>
    {
        routes.MapRoute(
           name: "default",
           template: "{controller=Home}/{action=Index}/{id?}");
    });
}
于 2019-09-03T16:22:45.197 回答