![](/img/trans.png)
[英]How to use (Microsoft.AspNetCore.TestHost)TestServer with Client Certificate
[英]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.