X509数字证书之三:Wcf服务端和客户端证书

摘要:X509数字证书可以用于Wcf服务端和客户端通信时的身份验证;也就是不仅是服务端进行身份认证,客户端在向服务端发起请求时,也需验证客户端的身份是否合法。证书的配置通常是在服务端和客户端的web.config文件中配置。要注意Makecert.exe生成的X509证书存储的位置是在LocalMachine存储位置。


在本示例中,服务端证书是WosServer, 客户端证书是WosClient(如何生成X509证书文件,请大家参考该系列的第一篇文章:X509数字证书之一:加密和解密),分别给出服务端web,config文件,客户端web.config文件的关于服务配置部分的内容。

1. 证书(本地计算机)

证书文件的读写权限可以在“管理私钥”的菜单位置进行配置,一般涉及到的用户用IIS 应用程序池用户,测试时候也可以是Everyone身份用户。



2. 服务端web.config 内容

要配置的主要节点信息如下:

1> binding 类型: wsHttpBinding

2> binding安全消息节点属性clientCredentialType :Certificate

3> ServiceBehavious节点中certificateValidationMode 配置为:PeerTrust

4> serviceCertificate 节点配置:

     findValue="WosServer"

     storeLocation="LocalMachine"

     storeName="My"

     x509FindType="FindBySubjectName"

全部文件内容如下:

   
     
       
         
           
         

       

     

   

   
     
       
       
         
       

       
     

   

   
     
       
         
         
         
         
         
           
             
           

                                            storeLocation="LocalMachine"
                                storeName="My"
                                x509FindType="FindBySubjectName"
                                />
         

       

     

   

   
 


3. 服务端代码

   IService1.cs 文件

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);
    }

   Service1.svc.cs文件

    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
    }


4. 客户端web.config文件配置

需要注意的是DNS的配置必须是服务端证书名称和clientCertificate 的节点配置。

1> binding类型:wsHttpBinding

2> 安全模式:Message

3> 消息的客户端身份类型:Certificate

4> 终结点的Identity 的dns:WosServer //服务端x509证书名称

5> 终结点行为的客户端证书配置:

     findValue="WosClient" 

     x509FindType="FindBySubjectName" 

     storeLocation="LocalMachine" 

     storeName="My"


全部的配置文件如下:

 

   
     
                  openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
          maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
          messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
          allowCookies="false">
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                        enabled="false" />
         
           
                                 algorithmSuite="Default" establishSecurityContext="true"/>
         

       

     

   

   
              binding="wsHttpBinding" bindingConfiguration="wsHttpBinding_IService1"
        contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" behaviorConfiguration="CustomBehavior" >
       
         
       

     

   

   
     
       
         
           
           
             
           

         

       

     

   

 


5. 客户端代码调用示例

Service1Client client = new Service1Client();
try
{
    client.Open();
    string s = client.GetData(10);
}
finally
{
    if (client != null && client.State != System.ServiceModel.CommunicationState.Closed)
        client.Close();
}
            
总结:本示例生成的证书和代码都是测试可以正常运行的,开发人员在编写wcf的应用时可以作为参考。


你可能感兴趣的:(网络安全,C#)