简单 WCF - X509 证书






4.92/5 (21投票s)
如何创建临时 X509 证书以及如何在 WCF 服务和客户端中实现 X509 证书安全
引言
本文将介绍如何创建临时 X509 证书,以及如何在 WCF 服务和客户端中实现 X509 证书安全。 此配置可以通过配置文件本身轻松完成。
Using the Code
创建 X509 证书
makecert.exe 工具将有助于创建 X509 证书。 此工具与 Microsoft .Net 2005 SDK 一起打包,也可以从微软网站下载。
步骤 1:创建临时证书 TempCA.cer
步骤 2:创建 SignedByCA.cer,该证书由 TempCA 证书进行数字签名和授权
步骤 3:使用 MMC 将 TempCA.cer 证书导入到“受信任的根证书颁发机构”文件夹(本地机器)
步骤 4:将 SignedByCA.cer 证书导入到个人文件夹
step1: makecert -n "CN=TempCA" -r -sv TempCA.pvk TempCA.cer
step2: makecert -sk SignedByCA -iv TempCA.pvk -n "CN=SignedByCA" -ic TempCA.cer SignedByCA.cer -sr localmachine -ss My
步骤 3:转到“开始”->“运行”->键入 MMC ->“文件”->“添加/删除管理单元”->“独立”选项卡->“添加”按钮->“证书”->“计算机帐户”

步骤 4:将 SignedByCA.cer 导入到个人文件夹

导出证书:要将创建的证书导出到外部世界,必须同时导出私钥。 这样可以将证书 .cer 和 .pvk 转换为 .pfx(例如,TempCA.cer 和 TempCA.pvk 转换为 TempCA.pfx)。 用于此转换的等效工具是 pvk2pfx.exe。 此工具将在您的 <programfile>/Visual Studio 8/Common7/Tools/bin/pvk2pfx.exe 中提供。
cmd> pvk2pfx.exe -pvk TempCA.pvk -spc TempCA.cer
this open wizard which will help to create .pfx file. Then you can distribute the PFX file to client
X509 证书的访问权限:如果 WCF 服务托管在 IIS 或 Windows 服务中,则根据托管环境,需要为 X509 证书提供特定的权限。 例如,如果 WCF 服务托管在 IIS 中,则必须授予 ASPNET 用户 (XP) 证书的权限。 因此,可以使用 Winhttpcertcfg.exe 来实现这一点,该工具将授予指定证书的权限。 该工具可以单独下载,也可以与本文一起下载。
//for grant ASPNET permission to TempCA.cer
cmd>winhttpcertcfg -g -c LOCAL_MACHINE\MY -s TempCA -a ASPNET
//for grant ASPNET permission to TEMPCA.pfx
cmd>winhttpcertcfg -i TempCA.pfx -g -c LOCAL_MACHINE\My -s TempCA -a ASPNET
WCF 服务器配置
配置 WCF 服务以支持 X509 证书作为安全过程之一。 可以在服务器配置文件中配置客户端级别的证书颁发机构。 因此,在使用 svcutil.exe 创建代理类时,将生成包含证书信息(令牌)的配置文件。
//web.config
<configuration>
<system.serviceModel>
<service name="service1" behaviorConfiguration="beh1">
<endpoint address="secure" contract="Icontract1" binding="wsHttpBinding" bindingConfiguration="binding1"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
<behaviors>
<serviceBehaviors>
<behavior name="beh1">
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceMetadata httpGetEnabled="true"/>
<serviceCredentials>
<!--certificate storage path in the server -->
<serviceCertificate findValue="TempCA" x509FindType="FindBySubjectName" storeLocation="LocalMachine" storeName="My"/>
<issuedTokenAuthentication allowUntrustedRsaIssuers="true"/>
<!--certificate storage path in the client -->
<clientCertificate>
<certificate findValue="TempCA" x509FindType="FindBySubjectName" storeLocation="LocalMachine" storeName="My"/>
</clientCertificate>
<userNameAuthentication userNamePasswordValidationMode="MembershipProvider"/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="beh1">
<clientCredentials>
<!--certificate storage path in the client -->
<clientCertificate findValue="TempCA" storeLocation="LocalMachine" x509FindType="FindBySubjectName" storeName="My"/>
<serviceCertificate>
<authentication certificateValidationMode="PeerOrChainTrust"/>
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="binding1">
<security mode="Message">
<!--security mode of certificate -->
<message establishSecurityContext="false" clientCredentialType="Certificate"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>
</configuration>
客户端配置:(使用 svcutil.exe 自动生成)
使用 svcutil.exe 生成的示例 WCF 客户端配置文件
<!--app.config-->
<client>
<endpoint address="https:///service1/servic1.svc/secure"
binding="wsHttpBinding" bindingConfiguration="bind1"
contract="Icontract1" name="WSHttpBinding_Icontract1">
<identity>
<certificate encodedValue="AwAAAAEAAAAUAAAAOTDk6LO4LsMQaY+65EgACb==" />
</identity>
</endpoint>
</client>
在成功实施上述步骤后,当从客户端向服务器发送任何请求时,消息将由客户端中存在的证书进行数字签名,服务器也会进行验证。
关注点
我对使用 WCF 开始安全实施很感兴趣
历史
2008 年 4 月 29 日。首次发布