且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为使用IdentityServer 4进行身份验证的AspNetCore API构建集成测试

更新时间:2023-12-01 15:04:16

以下建议是一个问题.原始源代码由于尝试构建WebHostBuilder

The below suggestion was one problem. The original source-code failed due to an exception by trying to build WebHostBuilder twice. Secondly the configuration-file was only present in the API project, not in the test-project, thats why authority wasn't set as well.

而不是这样做

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
   .AddJwtBearer(options =>
   {
       options.Authority = Configuration.GetValue<string>("IdentityServerAuthority");
       options.Audience = Configuration.GetValue<string>("IdentityServerAudience");
       options.BackchannelHttpHandler = BackChannelHandler;
   });

您必须执行以下操作:

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
   .AddIdentityServerAuthentication(options =>
   {
      options.Authority = Configuration.GetValue<string>("IdentityServerAuthority");
      options.JwtBackChannelHandler = BackChannelHandler;
    });

您可以找到一个示例希望对我有帮助!