简体   繁体   English

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

[英]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.

相关问题 如何使用 (Microsoft.AspNetCore.TestHost)TestServer 和客户端证书 - How to use (Microsoft.AspNetCore.TestHost)TestServer with Client Certificate 如何将证书附加到 DelegatingHandler 的 InnerHandler - How to attach a certificate to the InnerHandler of a DelegatingHandler 从Microsoft.AspNetCore.TestHost.TestServer获取连接字符串值 - Get connection string value from Microsoft.AspNetCore.TestHost.TestServer 在 AspNetCore 与 TestServer 的集成测试中模拟和解决 Autofac 依赖项 - Mocking and resolving Autofac dependency in integration test in AspNetCore with TestServer 为什么 TestServer (AspNetCore) 在静态文件上给出 404 错误? - Why the TestServer (AspNetCore) gives 404 error on static files? 配置AspNetCore TestServer返回500而不是引发异常 - Configure AspNetCore TestServer to return 500 instead of throwing exception 带有客户端证书身份验证的.Net Core Web API - .Net Core Web API with Client Certificate Authentication 在不使用Microsoft.AspNetCore.TestHost中包含的TestServer的情况下运行Kestrel进行测试 - Running Kestrel for testing without using the TestServer included in Microsoft.AspNetCore.TestHost 迁移问题 AspNetCore API - Migration Issue AspNetCore API 如何将TestServer output登录到测试控制台 - How to log TestServer output to the test console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM