首页 新闻 搜索 专区 学院

HttpClient 发送certificates,但HttpContext.Connection.ClientCertificate是空

0
悬赏园豆:10 [已解决问题] 解决于 2021-09-20 21:03

使用HttpClient发送带有certificates的请求后,无法在api端获取。
客户端
var cert = new X509Certificate2(@"D:\upload\images\证书.pfx", "123");
var handler = new HttpClientHandler() { ClientCertificateOptions = ClientCertificateOption.Manual };
handler.ClientCertificates.Add(cert);

        var client = new HttpClient(handler);

        var request = new HttpRequestMessage()
        {
            RequestUri = new Uri("https://localhost:44314/home/get"),
            Method = HttpMethod.Get,

        };
        var response = client.SendAsync(request).GetAwaiter().GetResult();
        if (response.IsSuccessStatusCode)
        {
            var responseContent = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
        }

接收
var clientCertInRequest = Request.HttpContext.Connection.ClientCertificate;
clientCertInRequest 一直是空

熊猫~·的主页 熊猫~· | 初学一级 | 园豆:188
提问于:2020-12-11 17:49
< >
分享
最佳答案
0

参考代码:
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
CloseableHttpClient httpClient = (CloseableHttpClient) requestFactory.getHttpClient();

TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;
SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, ALLOW_ALL_HOSTNAME_VERIFIER);
httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 8443, sf));

ResponseEntity<String> response = new RestTemplate(requestFactory).
  exchange(urlOverHttps, HttpMethod.GET, null, String.class);
收获园豆:10
架构师修炼之道 | 菜鸟二级 |园豆:261 | 2020-12-11 19:37

具体一点,是httpclient这边的问题还是server的问题?查了很多博客,要把pfx加到store里,不大了解?

熊猫~· | 园豆:188 (初学一级) | 2020-12-14 17:47
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册