繁体   English   中英

如何将客户端证书附加到AspNetCore TestServer api?

[英]How to attach client certificate to AspNetCore TestServer api?

我期待在Microsoft.AspNetCore.TestHost.TestServer在微软的源代码。 在那里,我看到CreateClient()属性是HttpClient对象。

那么如何在xUnit Test中附加Digitial Signature的客户端证书?

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但它也不支持该证书选项。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM