【问题标题】:How to attach client certificate to AspNetCore TestServer api?如何将客户端证书附加到 AspNetCore TestServer api?
【发布时间】:2018-03-18 11:24:26
【问题描述】:

我正在查看 Microsoft 源代码中的 Microsoft.AspNetCore.TestHost.TestServer。在那里我看到CreateClient() 属性是HttpClient 对象。

那么如何在 xUnit Test 中附加数字签名的客户端证书?

HttpClient 就是这个例子。

var x509Certificate2 = new X509Certificate();  // Pretend it contains certificate data already.

var httpClientHandler = new HttpClientHandler() {
    ClientCertificateOptions = ClientCertificateOption.Manual
};
httpClientHandler.ClientCertificates.Add(x509Certificate2);

using (var httpClient = new HttpClient(httpClientHandler))
{
}

现在使用TestServer

var testServer = new TestServer();

testServer.CreateClient();  // How do I attached client certificate here?

那么,我们如何在此处附加客户端证书?对于CreateClient()

另外,我可以尝试实现HttpRequestMessage,但它也不支持该证书选项。

【问题讨论】:

  • 在这方面也需要帮助 :)

标签: asp.net-core .net-core asp.net-core-2.0


【解决方案1】:

您可以尝试使用 testServer.SendAsync(...) 方法并从那里构造您的 HttpContext。

var result = await server.SendAsync(context =>
                                    {
                                        context.Connection.ClientCertificate = myClientCertificate;
                                        context.Request.Method = "POST";
                                     });

只要确保根据需要指定 Path 和 Body。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 2017-10-18
    • 2015-10-26
    • 1970-01-01
    • 2021-05-29
    • 2020-03-04
    • 2015-02-14
    相关资源
    最近更新 更多