一、自建根证书web
makecert -r -pe -n "CN=WebSSLTestRoot" -b 12/22/2013 -e 12/23/2024 -ss root -sr localmachine -len 2048app
二、建网站用的证书网站
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 localmachinespa
cn是网站对应的域名3d
三、建客户端证书code
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 2048blog
cn能够是用户名ssl
根证书在 本地计算机-受信任的根证书颁发机构get
网站证书在 本地计算机-Web宿主cmd
客户端证书在 当前用户-我的
iis中,创建网站用web宿主的
使用ie,访问,当客户端有证书试,会要求选择证书
string r; HttpClientCertificate cert = Request.ClientCertificate; if (cert.IsPresent) r = cert.ServerSubject + "---" + cert.Subject; else r = "No certificate was found."; return Content(r);
咱们能够经过代码来获取证书信息
四、程序自动建立用户客户端证书
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身份运行
五、导出,并提供客户下载
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”那部分
六、iis的ssl设置
若是是必需,当客户端没有证书时
当选择
没有证书则显示
对应上面代码的else分支
选择 忽略 则不会要求客户端提供证书了
自建根,当客户端访问时,须要导入根证书到受信任的根