还剩1页未读,继续阅读
文本内容:
谈谈的客户端认证WCF前面介绍认证和用户名/密码认证这两种典型的客户端认证模式,我们最后来介绍最Windows后一种客户端认证方式,即客户端凭证类型为证书时服务端采用的认证,简称为证书认X.509证我们照例先看看看客户端证书凭证如何设置设置
一、客户端证书凭证的设置在服务认证一文中,我们知道了基于证书证书的服务凭证通过X.509类型表示与之对应地,客户端凭证对应的类型是X509CertificateRecipientServiceCredential如下面的定义所示,在终结点行为中,X509CertificatelnitiatorClientCredentialo Clientcredentials具有一个只读的属性,其类型就是该类Clientcertificate X509CertificatelnitiatorClientCredentialo型实际上是对一个类型对象的封装,我们可以通过两个方法重载X509Certificate2SetCertificate以证书引用的方式指定某个具体的证书作为客户端的凭证X.5091:public classClientcredentials:SecurityCredentialsManager;lEndpointBehavior2{〃其他成员3:4:public X509CertificatelnitiatorClientCredential Clientcertificate{get;}5}6:public sealedclass X509CertificatelnitiatorClientCredential7{〃其他成员8:9:public voidSetCertificatestring subjectName,StoreLocation storeLocation,StoreName storeName;10:public voidSetCertificateStoreLocation StoreLocation,StoreName StoreName,X509FindType findType,object findValue;11:public X509Certificate2Certificate{get;set;}12}在下面给出服务调用代码中,我们通过编程的方式为设置了一个基ChannelFactoryTChannel于证书的客户端凭证X.5091:using ChannelFactoryICalculatorchannelFactory=newChannelFactoryICalculatorcalculatorService23:channelFactory.Credentials.ClientCertificate.SetCertificate4:StoreLocation.LocalMachine,5:StoreName.TrustedPeople,6:X509FindType.FindBySubjectName,7:Foo;8:Calculator calculator=channelFactory.CreateChannel;9:double result=calculator.Addl,2;10://...11:}终结点行为同样为客户端默认证书的设置定义相应的配置在下面给出的Clientcredentials片断中,我们通过配置的方式为终结点凭证指定了一个证书这个证书将作为服务XML X.509代理对象(通过创建的代理,或者是直接实例化通过元数据导入生ChannelFactoryTChannel成的服务代理类型)的默认客户凭证,而该凭证可以通过编程进行动态更改1:system.serviceModel2:...3:behaviors4:endpointBehaviors5:behavior name=defaultClientCert6:clientCredentials7:clientCertificate storeLocation=LocalMachine storeName=Myx509FindType=FindBySubjectName findValue=Foo78:/clientCredentials9:/behavior10:/endpointBehaviors11:/behaviors12:/system.serviceModel
二、客户端证书认证模式关于基于证书的客户端认证,你可以与基于证书的服务认证进行类比对于服务认证,服务在寄宿的时候指定某个证书作为服务的凭证客户端在默认的情况下会以模式对X.509ChainTrust服务证书进行认证,也可以通过这个终结点行为指定不同的认证模式对于Clientcredentials客户端认证,认证方变成了服务本身,服务端对客户证书的认证也采用相同的策略:默认认证模式为但借助这个服务行为来指定不同的认证模式ChainTrust,Servicecredentials下面给出了服务端进行客户端证书认证模式设置对应的相关类型的定义服务行为中具有一个只读的属性,类型为Servicecredentials Clientcertificate(对应于服务认证中的X509CertificatelnitiatorServiceCredential)X509CertificateRecipientClientCredential定义了只读属性其类型为X509Certificatelnitiator Authentication,(对应于服务认证中的)X509ClientCertificateAuthentication X509ServiceCertificateAuthentication和定义类似,你可以通过o X509ServiceCertificateAuthentication设置不同的认证模式(、X509ClientCertificateAuthentication NonePeerTrustChainTrust)当你选择模式的时候,你需要通过PeerOrChainTrustCustom oCustom属性设置一个自定义的CustomCertificateValidator X509CertificateValidatoro1:public classServicecredentials:SecurityCredentialsManager,IServiceBehavior2{〃其他成员3:4:public X509CertificatelnitiatorServiceCredential Clientcertificate{get;}5}6:public sealedclass X509CertificatelnitiatorServiceCredential7:{〃其他成员8:9:public X509ClientCertificateAuthentication Authentication{get;}10:public X509Certificate2Certificate{get;set;}11:}12:public classX509ClientCertificateAuthentication13:{〃其他成员14:15:public X509CertificateValidationMode CertificateValidationMode{get;set;}16:public X509CertificateValidator CustomcertificateValidator{get;set;}17下面的代码演示了在对服务进行自我寄宿的情况下,如何设置对客户端证书进行认证的模式,在这里将认证模式设置成了PeerOrChainTrusto1:using ServiceHosthost=new ServiceHosttypeofCalculatorService23:Servicecredentials servicecredentials=host.Description.Behaviors.FindServiceCredentials;4:if null==servicecredentials56:servicecredentials=new ServiceCredentials;7:host.Description.Behaviors.AddserviceCredentials;89:servicecredentials.Clientcertificate.Authentication.CertificateValidationMode=X509CertificateValidationMode.PeerOrChainTrust;10:host.Openf;11://...12:}我们推荐的依旧是采用配置的方式,上面这段代码中设置的服务行为可以通过下面一端配置来表不1:system.serviceModel2:...3:behaviors4:serviceBehaviors5:behavior name=setCertAuthentication6:serviceCredentials7:clientCertificate8:authenticationcertificateValidationMode=PeerOrChainTrust79:/clientCertificate10:/serviceCredentials11:/behavior12:/serviceBehaviors13:/behaviors14:/system.serviceModel。