iis https 客户端证书

1、自建根证书

makecert -r -pe -n "CN=WebSSLTestRoot" -b 12/22/2013 -e 12/23/2024 -ss root -sr localmachine -len 2048

 

2、建网站用的证书

makecert -pe -n "CN=www.aaa.com" -b 12/22/2013 -e 12/23/2024 -eku 1.3.6.1.5.5.7.3.1 -is root -ir localmachine -in WebSSLTestRoot -len 2048 -ss WebHosting -sr localmachine

cn是网站对应的域名

3、建客户端证书

makecert -pe -n "CN=czcz1024" -eku 1.3.6.1.5.5.7.3.2 -is root -ir localmachine -in WebSSLTestRoot -ss my -sr currentuser -len 2048

cn可以是用户名

根证书在 本地计算机-受信任的根证书颁发机构

网站证书在 本地计算机-Web宿主

客户端证书在 当前用户-个人

 

iis中,建立网站用web宿主的

使用ie,访问,当客户端有证书试,会要求选择证书

image

string r;
HttpClientCertificate cert = Request.ClientCertificate;
if (cert.IsPresent)
    r = cert.ServerSubject + "---" + cert.Subject;
else
    r = "No certificate was found.";
return Content(r);

我们可以通过代码来获取证书信息

 

image

 

4、程序自动创建用户客户端证书

var path = @"…\makecert.exe";
var cmd = " -pe -n \"CN=czcz1024\" -eku 1.3.6.1.5.5.7.3.2 -is root -ir localmachine -in WebSSLTestRoot -ss my -sr currentuser -len 2048";
var p = Process.Start(path, cmd);
p.WaitForExit();            
p.Close();

调用cmd去执行,需要iis已administrator身份运行

image

5、导出,并提供客户下载

X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.MaxAllowed);
foreach (X509Certificate2 myX509Certificate2 in store.Certificates)
{
    if (myX509Certificate2.Subject == "CN=czcz1024")
    {
        byte[] CertByte = myX509Certificate2.Export(X509ContentType.Pfx,"123456");
        return File(CertByte, "application/octet-stream", "cert.pfx");
    }
}

return Content("not found");

同样,这个也需要administrator才能获取到

需要导出成pfx,客户端才可以导入,导入的时候,需要密码,密码为代码中的“123456”那部分

6、iis的ssl设置

image

如果是必需,当客户端没有证书时

image

 

当选择

image

没有证书则显示

image

对应上面代码的else分支

 

选择 忽略 则不会要求客户端提供证书了

 

自建根,当客户端访问时,需要导入根证书到受信任的根

posted @ 2016-07-22 17:13  czcz1024  阅读(2518)  评论(1编辑  收藏  举报