如何将PFX安装的证书添加到Windows.Web.Http.HttpClient

时间:2017-12-15 06:50:21

标签: c# uwp httpclient pfx

我手动将pfx扩展证书安装到我的机器上。如何获取并传入Windows.Web.Http.HttpClient

我尝试了以下方式,但没有运气

    var myFilter = new HttpBaseProtocolFilter();
    CertificateQuery certQuery = new Windows.Security.Cryptography.Certificates.CertificateQuery();
    certQuery.FriendlyName = "TEST";    // This is the friendly name of the certificate that was just installed.
    IReadOnlyList<Certificate> certificates = await Windows.Security.Cryptography.Certificates.CertificateStores.FindAllAsync(certQuery);
    var client = new HttpClient(certificates[0]);

有人可以帮我解决这个如何在httpclient中添加手动安装的证书的问题吗?

1 个答案:

答案 0 :(得分:-1)

X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certs = store.Certificates.Find(
    X509FindType.FindByIssuerName, "TEST", false); 
    //Change FindType to your liking, it doesn't support FriendlyName, 
    //maybe use your method?

WebRequestHandler handler = new WebRequestHandler();
X509Certificate2 certificate = GetMyX509Certificate();
handler.ClientCertificates.Add(certificate);
HttpClient client = new HttpClient(handler);

你有它。也许使用您的证书进行Add方法,但我不知道它们是否兼容。