【问题标题】:Client Certificate is always null on server side客户端证书在服务器端始终为空
【发布时间】:2017-10-05 20:24:06
【问题描述】:

我阅读了很多关于如何发送客户端证书的帖子并做了所有这些,但它在服务器端是空的。

我在 mytest.aspx.cs 页面上编写了这段代码

 protected void Page_Load(object sender, EventArgs e)
   {
    string host = @"http://localhost:57855/Temp/index.aspx";
    string certName = @"C:\cert.pfx";
    string password = @"123456";

    try
    {

        X509Certificate2Collection certificates = new 
        X509Certificate2Collection();

        certificates.Import(certName, password, 
        X509KeyStorageFlags.MachineKeySet | 
        X509KeyStorageFlags.PersistKeySet);

        ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true;

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(host);
        req.AllowAutoRedirect = true;
        req.ClientCertificates = certificates;

        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        string postData = "login-form-type=cert";
        byte[] postBytes = Encoding.UTF8.GetBytes(postData);
        req.ContentLength = postBytes.Length;

        Stream postStream = req.GetRequestStream();
        postStream.Write(postBytes, 0, postBytes.Length);
        postStream.Flush();
        postStream.Close();
        WebResponse resp = req.GetResponse();

        Stream stream = resp.GetResponseStream();
        using (StreamReader reader = new StreamReader(stream))
        {
            string line = reader.ReadLine();
            while (line != null)
            {
                Console.WriteLine(line);
                line = reader.ReadLine();
            }
        }

        stream.Close();
    }
    catch (Exception ex)
    {
        //Console.WriteLine(e);
    }
}

在 index.aspx 页面中我写了这段代码

    protected void Page_Load(object sender, EventArgs e)
{
    bool b = false;
    if (HttpContext.Current.Request.ClientCertificate.IsPresent)
        b = true;//b is always  null

}

我也在使用 IIs express 。在 C:\Users\Administrator\Documents\IISExpress\config 的 applicationhost 文件中,我更改了两部分

 <security>

       <access sslFlags="SslNegotiateCert" />
      ....
      <authentication>
         <clientCertificateMappingAuthentication enabled="true" />

         <iisClientCertificateMappingAuthentication  enabled="true">
         </iisClientCertificateMappingAuthentication>
         .........
       </security>

我在 mmc=>Certificates/personal/certificates 中安装了 cert.pfx 和 mmc=>证书(当前用户)/个人/证书

但总是在索引页 b 为假。

另外我应该说 cert.pfx 不是 ssl 证书。它是一个数字签名证书,并且在证书的 enhanskeyusage 字段中具有客户端身份验证

【问题讨论】:

  • 为什么要在代码中加载 ssl 证书?你想使用 https 吗?
  • 我想用证书对客户端进行身份验证。我需要客户在他的 http/https 请求中附加一个证书(ssl 或数字签名证书......)

标签: c# certificate client iis-express client-certificates


【解决方案1】:

我在服务器中安装了客户端证书吊销列表并解决了

【讨论】:

  • 我无法完成这项工作,所以如果你能确认这些,那就太好了:(1) 客户端和服务器证书需要来自同一个 CA,(2) IIS 应该有SSL 设置切换到Accept。使用您的设置,我的网络应用程序无法加载。我收到 500 HTTP 错误代码。这可以与表单身份验证结合使用吗?
猜你喜欢
  • 1970-01-01
  • 2014-05-28
  • 1970-01-01
  • 2014-09-05
  • 2021-10-03
  • 2020-10-23
  • 1970-01-01
  • 2015-10-20
  • 1970-01-01
相关资源
最近更新 更多