gpt4 book ai didi

C# Rest 调用通过证书

转载 作者:太空宇宙 更新时间:2023-11-03 14:12:32 26 4
gpt4 key购买 nike

我有一个受证书保护的 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,并将其 ClientCertificateOptions 属性设置为 Automatic。生成的 HttpClient 然后可以正常使用:如果在连接握手期间服务器需要客户端证书,HttpClientHandler 实例将自动为用户的个人证书存储选择兼容的客户端证书。

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

最佳答案

能够使用以下代码解除我的证书:

    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;
}

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

关于C# Rest 调用通过证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27434511/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com