【问题标题】:XUnit Integration Testing with Identityserver: Token received from Identityserver is unable to match keyXUnit 与 Identityserver 的集成测试:从 Identityserver 收到的令牌无法匹配密钥
【发布时间】:2021-02-09 18:40:35
【问题描述】:

我目前正在尝试在具有 2 个服务器的环境中实施集成测试:

  • .NET Core API 服务器
  • IdentityServer4 身份验证服务器

经过一番努力,我设法让两者相互通信,但是,IdentityServer 在尝试确认 JWT 令牌(通过 API)时抛出以下错误:

承载未通过身份验证。失败消息:IDX10501:签名 验证失败。无法匹配键:孩子: '8C1D5950D083E20D4B20DE9B37AC71FAEF679469'。

我会尽量保持代码示例简短:

在 XUnit 启动时,我为两个 TestServer 配置和创建客户端。

    public APITestBase(ITestOutputHelper output)
    {
        _output = output;

        var apicon = new ConfigurationBuilder()
            .AddJsonFile("apisettings.json", optional: true, reloadOnChange: true)
            .Build();

        var authcon = new ConfigurationBuilder()
            .AddJsonFile("authsettings.json", optional: true, reloadOnChange: true)
            .Build();

        _authServer = new TestServer(new WebHostBuilder()
            .UseConfiguration(authcon)
            .ConfigureLogging(x => x.AddXUnit(_output))
            .UseEnvironment("Development")
            .UseStartup<Auth.Startup>());

        _authClient = _authServer.CreateClient();

        _server = new TestServer(new WebHostBuilder()
            .UseConfiguration(apicon)
            .ConfigureLogging(x => x.AddXUnit(_output))
            .UseEnvironment("Test")
            .ConfigureServices(
                  services => {
//Here, I'm adding an httpclienthandler. In the application I will use this as JWTBackChannelHandler. This allows communication between the two servers
                      services.AddSingleton<HttpMessageHandler>(_authServer.CreateHandler());
                  })
            .UseStartup<Api.Startup>());

        _client = _server.CreateClient();

        APIConfig = new Cnfg();
         
        apicon.Bind("APIConfig", APIConfig);

        //Make users in api

    }

这是我正在尝试运行的实际测试

        [Fact]
        public async Task ApplyTest()
        {
            _client.SetBearerToken(await GetAccessToken());

            // Act
            var response = await _client.GetAsync($"{APIConfig.ApiBaseUrl}api/call/");
             response.EnsureSuccessStatusCode();
        }

这里是 GetAccessToken,它返回集成用来将自己标识为用户的 JWT 令牌……至少这是计划。

        protected async Task<string> GetAccessToken()
    {
        var disco = await _authClient.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest()
        {
            Address = _authServer.BaseAddress.ToString(),
            Policy =
                {
                    ValidateIssuerName = false
                }
        });

        if (!String.IsNullOrEmpty(disco.Error))
        {
            throw new Exception(disco.Error);
        }
        var response = await _authClient.RequestPasswordTokenAsync(new PasswordTokenRequest
        {
            Address = disco.TokenEndpoint,
            GrantType = "password",
            ClientId = "api",

            UserName = "test@test.nl",
            Password = "test"
        });

        return response.AccessToken;
    }

我可以发现通过此函数生成的 JWT 令牌与正常网站请求之间唯一可识别的区别是缺少公钥(根据 jwt.io)。

最后,因为我认为这一点可能有用,这里是 API 的 Startup.cs 中我强制使用 JWTBackChannelHandler 的部分

if (Environment.EnvironmentName == "Test")
            {
                services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
                    .AddIdentityServerAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme, o =>
                    {
                        o.JwtBackChannelHandler = _identityServerMessageHandler;
                        o.RequireHttpsMetadata = false;
                    })
                    .AddApiKeyAuthenication(o =>
                    {
                        o.Keys.Add("****".ToSha256());
                        o.Keys.Add("****".ToSha256());
                    });
            }

提前非常感谢您的帮助,我在这个问题上已经走到了尽头。如果您想了解更多信息,请告诉我,我很乐意提供。

【问题讨论】:

    标签: asp.net-core jwt identityserver4 xunit testserver


    【解决方案1】:

    又挖了 2 个小时后发现需要添加

    o.Authority = identityServerOptions.Address.AbsoluteUri;
    

    到 AddIdentityServerAuthentication。

    感谢那些花时间为我深入研究问题的人。

    【讨论】:

      猜你喜欢
      • 2013-09-07
      • 2016-09-29
      • 2017-02-02
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      • 2016-11-13
      • 2016-10-07
      • 1970-01-01
      相关资源
      最近更新 更多