【问题标题】:SSl certificate details in Web.config of Rest Client C#Rest Client C# 的 Web.config 中的 Ssl 证书详细信息
【发布时间】:2017-04-20 22:08:42
【问题描述】:

在 WCF 或 Web 服务中,我们在客户端凭据标签中添加证书的详细信息,如下所示:

<clientCredentials>
    <clientCertificate storeLocation="LocalMachine" x509FindType="FindBySubjectName" storeName="My" findValue="XYZ" />
</clientCredentials>

但是我们如何在 Rest Client Case 中配置它,我们只需要访问一个 RestFul 服务的 URI。

【问题讨论】:

标签: c# rest wcf


【解决方案1】:

您可以像这样将客户端证书添加到 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); 

注意:WebRequest已过时。

尝试改用 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);

【讨论】:

  • 但是有没有办法从 web.config 读取证书详细信息,例如商店名称、位置、值等?我必须使细节可配置
  • 是的,只需使用 ConfigurationManager 将它们传入,或者您阅读配置文件即可
  • 感谢 @Alex 我能够传入商店名称并找到证书的值,但无法为查找类型和商店位置配置它
  • 将它们作为 appSettings 存储在您的配置文件中,然后使用 ConfigurationManager.AppSettings
  • 是但不能在 X509Store 构造函数中传递字符串值我们只能传递一个 StoreLocation 枚举
猜你喜欢
  • 1970-01-01
  • 2011-12-13
  • 1970-01-01
  • 2013-07-18
  • 1970-01-01
  • 2012-07-06
  • 2021-11-29
  • 2021-09-12
相关资源
最近更新 更多