[英]SSl certificate details in Web.config of Rest Client C#
In WCF or web services we add details of certificate in client credential tag as below: 在WCF或Web服务中,我们在客户端凭据标签中添加证书的详细信息,如下所示:
<clientCredentials>
<clientCertificate storeLocation="LocalMachine" x509FindType="FindBySubjectName" storeName="My" findValue="XYZ" />
</clientCredentials>
But how can we configure it in Rest Client Case where we just need to access a URI of RestFul service.? 但是,我们如何在Rest Client Case(仅需要访问RestFul服务的URI)中配置它呢?
You can add a client certificate to a web request like this. 您可以像这样将客户端证书添加到Web请求中。
X509Store store = new X509Store("My", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certificates = store.Certificates.Find(X509FindType.FindBySubjectKeyIdentifier, "XYZ", true);
X509Certificate2 certificate = certificates[0];
HttpWebRequest request = new HttpWebRequest();
request.ClientCertificates.Add(certificate);
Note: WebRequest
is obsolete. 注意: WebRequest
已过时。
Try using HttpClient instead, which would look something like this 尝试使用HttpClient代替,它看起来像这样
X509Store store = new X509Store("My", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certificates = store.Certificates.Find(X509FindType.FindBySubjectKeyIdentifier, "XYZ", true);
X509Certificate2 certificate = certificates[0];
WebRequestHandler handler = new WebRequestHandler();
handler.ClientCertificates.Add(certificate);
HttpClient client = new HttpClient(handler);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.