客户端证书在服务器端始终为空

时间:2017-10-03 12:34:06

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

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

我在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 =&gt;证书/个人/证书中安装了cert.pfx mmc =&gt;证书(当前用户)/个人/证书

但始终在索引页面b中为false。

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

1 个答案:

答案 0 :(得分:0)

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