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分支

 

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

 

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

转载于:https://www.cnblogs.com/czcz1024/p/5695842.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值