簡體   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