如何添加客户端证书中的ASP.NET Web API的内存测试? [英] How to add client certificate in ASP.NET Web API in-memory testing?

查看:91
本文介绍了如何添加客户端证书中的ASP.NET Web API的内存测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用测试我的Web API服务的内存的HttpServer

I want to test my Web API service using in-memory HttpServer.

目前的设置如下所示:

 var httpConfig = CreateTestHttpConfiguration();
 var server = new HttpServer(httpConfig);

 var handler = new WebRequestHandler();
 var cert = new X509Certificate2("filename", "password");
 handler.ClientCertificates.Add(cert);
 var client = HttpClientFactory.Create(handler, server);

我可以用这些客户端向服务器请求和一切正常,只是证书不被添加到请求。

I can make requests to the server using these client and everything works except that certificate is not added to the request.

据我了解这种情况发生,因为服务器执行处理程序(我不能,因为他们实现不同的重新排列接口)和自服务器立即回应处理程序甚至不执行(我使用测试这种假设 HttpClientHandler 的子类,而不是处理程序)。

As I understand that happens since server executes before handler (I can't rearrange them since they implement different interfaces) and since server immediately responses handler is not even executed (I've tested this assumption using HttpClientHandler subclass instead of handler).

所以我的问题是:我如何添加客户端证书的内存测试

推荐答案

此方法将做到这一点:

        var server = new HttpServer(configuration);
        var invoker = new HttpMessageInvoker(server);
        var certificate = GetCertificate();

        var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/YourPath");
        request.Properties[HttpPropertyKeys.ClientCertificateKey] = certificate;
        var result = await invoker.SendAsync(request, CancellationToken.None);

这篇关于如何添加客户端证书中的ASP.NET Web API的内存测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆