Identityserver4 IdentityServerBuilderConfiguration Extension中Blazor WASM托管的身份验证空引用异常

Identityserver4 IdentityServerBuilderConfiguration Extension中Blazor WASM托管的身份验证空引用异常,identityserver4,blazor,blazor-hosted,Identityserver4,Blazor,Blazor Hosted,我有一个blazor WASM托管的项目,使用IdentityServer 4(VS模板的默认设置)。然而,当我启动我的应用程序时,我得到了以下错误。调试显示options.Value.SigningCredential为null,因此.Key导致NullReferenceException。所以我错过了一些东西,在某个地方 这是Client Program.cs public static async Task Main(string[] args) {

我有一个blazor WASM托管的项目,使用IdentityServer 4(VS模板的默认设置)。然而,当我启动我的应用程序时,我得到了以下错误。调试显示options.Value.SigningCredential为null,因此.Key导致NullReferenceException。所以我错过了一些东西,在某个地方

这是Client Program.cs

public static async Task Main(string[] args)
        {
            var builder = WebAssemblyHostBuilder.CreateDefault(args);
            //builder.RootComponents.Add<App>("#app");

            builder.Services.AddHttpClient("BBQFriend.API", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
                .AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();

            // Supply HttpClient instances that include access tokens when making requests to the server project
            builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("BBQFriend.API"));

            builder.Services.AddApiAuthorization();

            var baseAddress = new Uri("https://localhost:44395/api/");

            void RegisterTypedClient<TClient, TImplementation>(Uri apiBaseUrl)
                where TClient : class where TImplementation : class, TClient
            {
                builder.Services.AddHttpClient<TClient, TImplementation>(client =>
                {
                    client.BaseAddress = apiBaseUrl;
                });
            }

            RegisterTypedClient<ICountryService, CountryService>(baseAddress);          

            await builder.Build().RunAsync();
        }
公共静态异步任务主(字符串[]args)
{
var builder=WebAssemblyHostBuilder.CreateDefault(args);
//builder.RootComponents.Add(“#app”);
builder.Services.AddHttpClient(“BBQFriend.API”,client=>client.BaseAddress=新Uri(builder.hostenEnvironment.BaseAddress))
.AddHttpMessageHandler();
//向服务器项目发出请求时,提供包含访问令牌的HttpClient实例
builder.Services.addScope(sp=>sp.GetRequiredService().CreateClient(“BBQFriend.API”);
builder.Services.AddApiAuthorization();
var baseAddress=新Uri(“https://localhost:44395/api/");
无效RegisterTypedClient(Uri apiBaseUrl)
where TClient:class其中TImplementation:class,TClient
{
builder.Services.AddHttpClient(客户端=>
{
client.BaseAddress=apiBaseUrl;
});
}
RegisterTypedClient(基本地址);
等待builder.Build().RunAsync();
}
这是Server Startup.cs

public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //Register EntityFramework Core Datacontext for Dependency Injection
            services.AddDbContext<DataContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));            

            //Add common Identity Screens
            services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
                .AddEntityFrameworkStores<DataContext>();

            //Set up IdentityServer
            services.AddIdentityServer()
                .AddApiAuthorization<ApplicationUser, DataContext>();

            services.AddAuthentication()
                .AddIdentityServerJwt();

            //Register Services for DirectNavigation
            services.AddScoped<ICountryService, CountryService>();

            //Register Repositories for Dependency Injection
            services.AddScoped<ICountryRepository, CountryRepository>();

            services.AddDatabaseDeveloperPageExceptionFilter();

            services.AddControllersWithViews();
            services.AddRazorPages();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseWebAssemblyDebugging();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            //app.UseHttpsRedirection();
            app.UseBlazorFrameworkFiles();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseIdentityServer();
            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
                endpoints.MapControllers();
                //endpoints.MapFallbackToFile("index.html");
                endpoints.MapFallbackToPage("/_Host");
            });
        }
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。使用此方法向容器中添加服务。
public void配置服务(IServiceCollection服务)
{
//为依赖项注入注册EntityFramework核心Datacontext
services.AddDbContext(options=>options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”));
//添加通用标识屏幕
services.AddDefaultIdentity(options=>options.SignIn.RequireConfirmedAccount=true)
.AddEntityFrameworkStores();
//设置IdentityServer
services.AddIdentityServer()
.addapi授权();
services.AddAuthentication()
.AddIdentityServerJwt();
//为DirectNavigation注册服务
services.addScope();
//为依赖项注入注册存储库
services.addScope();
AddDatabaseDeveloperPageExceptionFilter();
services.AddControllersWithViews();
services.AddRazorPages();
}
//此方法由运行时调用。使用此方法配置HTTP请求管道。
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseWebAssemblyDebugging();
}
其他的
{
app.UseExceptionHandler(“/Error”);
//默认的HSTS值为30天。您可能希望在生产场景中更改此值,请参阅https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
//app.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseIdentityServer();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(端点=>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
//MapFallbackToFile(“index.html”);
endpoints.MapFallbackToPage(“/_主机”);
});
}
下面是应用程序DataContext.cs

public class DataContext : ApiAuthorizationDbContext<ApplicationUser>
    {
        public DataContext(DbContextOptions options, IOptions<OperationalStoreOptions> operationalStoreOptions) : base(options, operationalStoreOptions)
        {
            ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
        }

        public DbSet<Country> Countries { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ApplyConfiguration(new CountryConfiguration());

            base.OnModelCreating(modelBuilder);

        }
    }
公共类DataContext:ApiAuthorizationDbContext
{
公共DataContext(DbContextOptions选项,IOOptions operationalStoreOptions):基本(选项,operationalStoreOptions)
{
ChangeTracker.QueryTrackingBehavior=QueryTrackingBehavior.NotTracking;
}
公共数据库集国家{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
ApplyConfiguration(newcountryconfiguration());
基于模型创建(modelBuilder);
}
}

问题在于缺少客户端配置值。 当您使用
builder.Services.addapiaauthorization()时它尝试从以下位置加载配置:

默认情况下,应用程序的配置按照约定从_configuration/{client id}加载。按照惯例,客户端ID设置为应用程序的程序集名称。通过使用选项调用重载,可以将此URL更改为指向单独的端点