启用客户端证书在本地的IIS Express中:
将\YourSlnFolder\.vs\config\applicationhost.config文件中的<section name="access" overrideModeDefault="Deny" />
改为<section name="access" overrideModeDefault="Allow" />
。
<sectionGroup name="system.webServer">
...
<sectionGroup name="security">
...
<section name="access" overrideModeDefault="Allow" />
然后按照以下方式编辑您的Web.config:
<configuration>
<system.webServer>
<security>
<access sslFlags="SslRequireCert" />
</security>
</system.webServer>
</configuration>
启用IIS上的客户端证书:
在IIS管理器中访问网站并单击SSL设置。然后将应用程序设置为需要SSL和需要客户端证书。
![enter image description here](https://istack.dev59.com/y67EC.webp)
创建新证书:
启动VS开发人员命令提示符
根证书:
makecert.exe -r -n "CN=TestRootCertificate" -pe -sv TestRootCertificate.pvk -a sha1 -len 2048 -b 01/01/2017 -e 01/01/2030 -cy authority TestRootCertificate.cer
请输入您的密码。
创建证书吊销列表(CRL)
makecert -crl -n "CN=TestRootCertificate" -r -sv TestRootCertificate.pvk TestRootCertificate.crl
将Bundle文件转换成.pfx文件(pvk2pfx.exe需要安装VS2017的“桌面开发与C++”组件)
pvk2pfx.exe -pvk TestRootCertificate.pvk -pi {password} -spc TestRootCertificate.cer -pfx TestRootCertificate.pfx
从根证书获取客户端证书:
makecert.exe -ic TestRootCertificate.cer -iv TestRootCertificate.pvk -pe -sv localtestclientcert.pvk -a sha1 -n "CN=localtestclientcert" -len 2048 -b 01/01/2015 -e 01/01/2030 -sky exchange localtestclientcert.cer -eku 1.3.6.1.5.5.7.3.2
请输入密码。
pvk2pfx.exe -pvk localtestclientcert.pvk -pi {password} -spc localtestclientcert.cer -pfx localtestclientcert.pfx
导入证书。
启动mmc.exe。
File -> Add or Remove Snap-ins -> Certificates -> Add -> Computer account -> Local computer
Certificates (Local Computer) -> Personal -> Certificates -> Right click -> All tasks -> Import -> localtestclientcert.pfx
Certificates (Local Computer) -> Trusted Root Certification Authorities -> Certificates -> Right click -> All tasks -> Import -> RootCertificate.cer
用于浏览器身份验证:
File -> Add or Remove Snap-ins -> Certificates -> Add -> My user account
Certificates - Current User -> Personal -> Certificates -> Right click -> All tasks -> Import -> localtestclientcert.pfx
现在访问您的网站需要一个客户端证书,该证书由服务器信任:
![enter image description here](https://istack.dev59.com/fjx1B.webp)
如果您按照此指南进行操作并出现以下错误:
HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.
或者
HTTP Error 403.7 - Forbidden
The page you are attempting to access requires your browser to have a Secure Sockets Layer (SSL) client certificate that the Web server recognizes.
您可能需要重新启动计算机。请注意,仅关闭iisexpress进程或Visual Studio是不够的。可以通过解决500.19
错误来解决,但证书比较棘手,因此建议重新启动计算机。
如果出现The request was aborted: Could not create SSL/TLS secure channel
错误,则可能是由于应用程序池无法访问特定证书。
证书(本地计算机)-> 个人-> 证书-> localtestclientcert-> 右键单击-> 所有任务-> 管理私钥-> 添加IIS APPPOOL\YourWebSite
并授予完全控制权限。