【问题标题】:C# Rest call passing certificateC# Rest 调用通过证书
【发布时间】:2014-12-11 23:42:11
【问题描述】:

我有一个受证书保护的 RESTful 端点,该证书包含在我的 Windows 证书信任库和 IIS 中。我正在尝试将该证书发送到 RESTful 端点以进行身份​​验证并获得响应。

所以我的终点是:https://myrestful.ssl.endpoint/return/some/data

如果我尝试直接从浏览器访问端点,我会得到 401 no certificate chain in request,这是我期望尝试直接访问的结果。但是我没有尝试使用我的 .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,其中包含设置为 Automatic 的 ClientCertificateOptions 属性。 生成的 HttpClient 然后可以正常使用:如果在连接握手期间服务器需要客户端证书,则 HttpClientHandler 实例将自动为用户的个人证书存储选择兼容的客户端证书。

但是,由于我仍然收到 401 响应,似乎我没有向请求发送我需要的正确公共证书。因此,如果我在 IIS 中有一个通用名称为 my.first.cert 的证书 - 有没有人知道我应该在上面的代码中将此证书添加到我的客户端请求中的正确方法?

【问题讨论】:

    标签: c# asp.net api rest ssl


    【解决方案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;
        }
    

    所以我传入了我想要的证书的主题名称。连接到我的端点时仍有问题,但我可以在另一个问题中发布。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-03
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      相关资源
      最近更新 更多