[英]How to attach client certificate to AspNetCore TestServer api?
I'm looking at Microsoft.AspNetCore.TestHost.TestServer
in the Microsoft source code. 我期待在Microsoft.AspNetCore.TestHost.TestServer
在微软的源代码。 There I see the CreateClient()
property that is HttpClient
objects. 在那里,我看到CreateClient()
属性是HttpClient
对象。
So how do I attached the client certificate for Digitial Signature in xUnit Test? 那么如何在xUnit Test中附加Digitial Signature的客户端证书?
HttpClient
example would be this. 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))
{
}
Now using the TestServer
现在使用TestServer
var testServer = new TestServer();
testServer.CreateClient(); // How do I attached client certificate here?
So, how do we attach the client certificate here? 那么,我们如何在这里附上客户证书呢? For CreateClient()
对于CreateClient()
Also, I can try to make do with implementing the HttpRequestMessage
but it doesn't support that certificate option either. 此外,我可以尝试实现HttpRequestMessage
但它也不支持该证书选项。
You may try to use the testServer.SendAsync(...) method and just construct your HttpContext from there. 您可以尝试使用testServer.SendAsync(...)方法,并从那里构建您的HttpContext。
var result = await server.SendAsync(context =>
{
context.Connection.ClientCertificate = myClientCertificate;
context.Request.Method = "POST";
});
Just make sure to specify the Path and the Body as needed. 只需确保根据需要指定Path和Body。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.