我有一个RESTful端点受证书保护,该证书包含在我的Windows证书信任存储区和IIS中。我正在尝试将该证书发送到RESTful端点以进行身份验证并获得响应。
所以我的端点就像:https://myrestful.ssl.endpoint/return/some/data
如果我尝试直接从浏览器点击端点,我会在请求中获得401没有证书链,这是我期望尝试直接命中的。但是,我并不想尝试从我的.NET代码中获取,但我仍然遇到401错误。我目前的代码如下:
var endpoint = "https://myrestful.ssl.endpoint/return/some/data";
var client = new HttpClient(new HttpClientHandler
{
ClientCertificateOptions = ClientCertificateOption.Automatic
});
var response = client.GetAsync(endpoint).Result;
请注意,我在IIS中托管的应用程序中访问的RESTful端点托管在Tomcat服务器上。我从这个site中读到了以下内容:
第一个选项是使用HttpClientHandler实例显式配置HttpClient,其中ClientCertificateOptions属性设置为Automatic。 然后可以正常使用生成的HttpClient:如果在连接握手期间服务器需要客户端证书,HttpClientHandler实例将自动为用户的个人证书存储选择兼容的客户端证书。
然而,由于我仍然得到401响应,好像我没有向请求发送我需要的正确的公共证书。因此,如果我在IIS中使用了my.first.cert的公共名称 - 有没有人有正确的方法我应该在上面的代码中将此证书添加到我的客户端请求中?
答案 0 :(得分:1)
能够使用以下代码取消我的证书:
private static X509Certificate2 FindSubjectNameInStore(string subjectName,
StoreName name, StoreLocation location)
{
//creates the store based on the input name and location e.g. name=My
var certStore = new X509Store(name, location);
certStore.Open(OpenFlags.ReadOnly);
//finds the certificate in question in this store
var certCollection = certStore.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, subjectName, false);
certStore.Close();
return certCollection.Count > 0 ? certCollection[0] : null;
}
所以我传递了我想要的证书的主题名称。仍然遇到连接到我的终端的问题,但我可以在另一个问题中发布。