当我使用
@attribute [Authorize]
在blazor客户端应用程序的剃须刀页面上,它给我这个错误
System.InvalidOperationException:无法提供属性值 类型为“ AuthorizationPolicyProvider” 'Microsoft.AspNetCore.Components.PageDisplay + AuthorizeViewWithSuppliedData'。 没有类型的注册服务 “ 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");
}
}
有关此问题的任何帮助
答案 0 :(得分:2)
我只需要添加services.AddAuthorizationCore();
感谢KodiakMx
public void ConfigureServices(IServiceCollection services)
{
services.AddBlazoredLocalStorage();
services.AddAuthorizationCore();
services.AddScoped<AuthenticationStateProvider, ApiAuthenticationStateProvider>();
}
答案 1 :(得分:0)
您可能需要将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?}");
});
}