ssl - 无法通过 RestSharp 发送客户端证书

标签 ssl asp.net-web-api x509certificate restsharp client-certificates

我可以访问我的 token 并将其添加到 RestSharp RestClient

//Create rest client.
IRestClient client = new RestClient("https://localhost.fiddler:44300");

var webClientCertificate = GetWebClientCertificate();

//Don't ask why... I'm getting an abiguous ref here.
client.ClientCertificates = new System.Security.Cryptography.X509Certificates.X509Certificate2Collection(){webClientCertificate};

IRestRequest request = new RestRequest("api/get-secure-data", Method.GET);
var response = client.Execute(request);

获取WebClient证书:
public X509Certificate2 GetWebClientCertificate()
{
  //Access certificate store
  X509Store certificateStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
  certificateStore.Open(OpenFlags.ReadOnly);
  var certificateCollection = certificateStore.Certificates.Find(X509FindType.FindBySubjectName, "web-client",false);
  var webClientCertificate = certificateCollection[0];
  certificateStore.Close();

  return webClientCertificate;
}

我得到了证书,我可以看到它已添加到客户端。

服务器代码
    [HttpGet]
    [Route("api/get-secure-data")]
    [RequireHttps]
    public IHttpActionResult GetSecureData()
    {
        try
        {
            X509Certificate2 cert = Request.GetClientCertificate(); //returns null
            PublicKey clientKey = cert.PublicKey;

            return Ok(clientKey);
        }
        catch (Exception exception)
        {
            return InternalServerError(exception);
        }
    }

fiddler 显示没有身份验证头,我错过了什么?

最佳答案

首先,您只需要设置证书本身,集合已经存在,其次,您在使用证书进行签名或身份验证(加密)时无法关闭存储,因为您与上下文断开连接。

关于ssl - 无法通过 RestSharp 发送客户端证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35781191/

相关文章:

c# - 构建 Service Fabric Web Api 时出现错误,无法从 Fabric.StatelessServiceContext 转换为 Fabric.ServiceInitializationParameters

c# - 在 ASP.NET Core 6 Program.cs 中配置 EF

c# - ASP.NET Web API 删除返回 404

c# - 从 X509 Cert pfx 文件中获取字符串

ssl - heroku cloudflare 子域 SSL 设置

apache - htaccess - 在子文件夹中使用域强制 SSL 和 www

java - 弃用了使用 SSL 的嵌入式 Jetty 7.6

ssl - nginx - 爬虫/机器人没有 ssl

c# - 使用用户证书以 Web 形式签署数据的最佳方式

azure - 如何让 AzureRM 应用程序网关将 ACME .PEM 证书作为 AGW SSL 端到端配置中的 trust_root_certificates?