WCF分布式安全开发实践(9):消息安全模式之Windows身份验证:Message_Windows_NetTcpBinding

来源:互联网 发布:java面试项目经验案例 编辑:程序博客网 时间:2024/05/12 12:07
Posted on 2009-08-25 23:44 Frank Xu Lei 阅读(1594) 评论(3)  编辑 收藏 网摘 所属分类: WCF分布式安全开发实践, SOA and EAI
   今天继续WCF分布式安全开发实践(9):消息安全模式之Windows身份验证:Message_Windows_NetTcpBinding。本文介绍的内容主要是:主要是消息安全模式的Windows身份验证方式,基于NetTcpBinding绑定协议的实现过程。主要内容:基本概念,服务端配置、客户端配置、总结。   Windows 域验证的原理在WCF分布式安全开发实践(4):传输安全模式之Windows身份验证:Transport_Windows_NetTcpBinding 有简单介绍。  这里就不在重复。
【1】消息安全模式之NetTcpBinding客户端身份验证:
       消息安全模式之NetTcpBinding客户端身份验证不需要服务器证书。这里的客户端和服务器的验证由DC来完成。 这里使用TCP协议。建议安全上下文以后,使用共享安全上下文对SOAP消息进行加密和签名。但是采用Windows身份验证。也就是客户端提供Windows域账号和密码才可以访问此服务。

1.身份验证(服务器):Windows DC验证服务身份。
2.身份验证(客户端):客户端使用Windows域账户进行身份验证

   WCF消息安全模式之Windows客户端身份验证的架构如下:
    
    客户端建立TLS安全上下文以后,会使用商定的密码对消息签名,保证数据的安全和机密性,消息签名放置被篡改。
   这里客户端提供的是有效的Windows域账号和密码,进行验证。服务器也在Windows域中。不需要证书。新启用端口8003。
【3】服务端配置:
   服务器证书配置完成以后,我们来配置服务端相关的文件,这里简单。也可以使用代码来完成。
    (1)服务类定义:
    重复使用以前定义的服务类代码。 这里服务类就一个方法就是根据客户端的调用参数name来打印调用时间,代码如下:

//1.服务契约
    [ServiceContract(Namespace = "http://www.cnblogs.com/frank_xl/")]
    
public interface IWCFService
    {
        
//操作契约
        [OperationContract]
        
string SayHello(string name);

    }
    
//2.服务类,继承接口。实现服务契约定义的操作
    public class WCFService : IWCFService
    {
        
//实现接口定义的方法
        public string SayHello(string name)
        {
            Console.ForegroundColor 
= ConsoleColor.Yellow;
            Console.WriteLine(
"Hello! {0},Calling at {1} ", name, DateTime.Now.ToLongTimeString());
            
return "Hello! " + name;
        }
    }

    (2)消息安全模式配置:
       使用消息安全模式,采用客户端Windows身份验证策略,Message安全模式下的Windows验证方式配置信息如下:

    <bindings>
      
<netTcpBinding>
      
<binding name="BindingConfigration">
        
<security mode="Message">
          
<transport clientCredentialType="None"  protectionLevel="None"/>
          
<message clientCredentialType="Windows"/>
        
</security>
      
</binding>
    
</netTcpBinding>
    
</bindings>


这里允许启用安全协商和建立安全上下文。这个配置要应用到服务的终结点配置上。才会生效。
    (3)证书使用:
    这里不需要使用证书。客户端和服务器端都存在于Windows域控制器内,由DC提供验证。和传输安全的Windows验证一样。WCF分布式安全开发实践(4):传输安全模式之Windows身份验证:Transport_Windows_NetTcpBinding 。
    (4)这里我们不需要使用Tcp传输协议,直接配置即可,服务终结点的配置信息如下:

<service behaviorConfiguration="WCFService.WCFServiceBehavior" name="WCFService.WCFService" >
        
<endpoint 
          address
="WCFService" 
          binding
="netTcpBinding" 
          bindingConfiguration
="BindingConfigration"
          contract
="WCFService.IWCFService">
          
<identity>
            
<dns value="computer"/>
          
</identity>
        
</endpoint>
        
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
        
<host>
          
<baseAddresses>
            
<add baseAddress="net.tcp://localhost:8003/"/>
          
</baseAddresses>
        
</host>
      
</service>
    
</services>
    
<behaviors>
      
<serviceBehaviors>
        
<behavior name="WCFService.WCFServiceBehavior">
          
<serviceMetadata httpGetEnabled="false" />
          
<serviceDebug includeExceptionDetailInFaults="false" />
        
</behavior>
      
</serviceBehaviors>
    
</behaviors>

   我们的服务元数据终结点使用基地址:net.tcp://localhost:8003/。

【4】客户端配置:
    这个过程和之前的安全模式添加服务引用的方式一样,参考WCF分布式安全开发实践(7):消息安全模式之匿名客户端:Message_None_WSHttpBinding, 添加服务的过程一样。直接引用。
    (1)引用元数据:
    因为服务的元数据交换节点启用了tcp协议,我们在客户端项目添加元数据地址net.tcp://localhost:8003/mex查找服务信息的时候,界面如下:
    
   继续就会添加完毕服务引用。过程和普通的添加服务元数据引用一样,会产生客户端相关代码文件。输入命名空间,现在我们点击Ok。等待完成即可。  
    (2)配置文件:
    客户端配置文件使用默认设置,主要是安全模式的设置要如下,与服务端匹配。使用传输安全是设置None方式。消息安全是Windows。代码如下:

<system.serviceModel>
        
<bindings>
            
<netTcpBinding>
              
<binding name="NetTcpBinding_IWCFService" >
                
<security mode="Message">
                  
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                  
<message clientCredentialType="Windows" />
                
</security>
              
</binding>
            
</netTcpBinding>
        
</bindings>
        
<client>
            
<endpoint address="net.tcp://localhost:8003/WCFService" binding="netTcpBinding"
                bindingConfiguration
="NetTcpBinding_IWCFService" contract="ClientProxy.IWCFService"
                name
="NetTcpBinding_IWCFService">
                
<identity>
                    
<dns value="computer" />
                
</identity>
            
</endpoint>
        
</client>
    
</system.serviceModel>

    (3)测试代码:
      我们这里就直接生成客户端代理类的实例来调用服务进行测试。这里客户端在调用服务以前,   客户端要提供Windows账号和密码,还有域。然后才能通过客户端代理来调用WCF服务。代码如下:

////HTTP WSHttpBinding_IWCFService1
            WCFClient.ClientProxy.WCFServiceClient wcfServiceProxy = new WCFClient.ClientProxy.WCFServiceClient("NetTcpBinding_IWCFService");
            
//通过代理调用SayHello服务
            string sName = "Frank Xu Lei Messaage Windows NetTcpBinding";
            
string sResult = string.Empty;
            wcfServiceProxy.ClientCredentials.Windows.ClientCredential.UserName 
= "FrankXuLei";
            wcfServiceProxy.ClientCredentials.Windows.ClientCredential.Password 
= "00000000";
            wcfServiceProxy.ClientCredentials.Windows.ClientCredential.Domain 
= "frankxulei.com";
            sResult 
= wcfServiceProxy.SayHello(sName);
            Console.WriteLine(
"Returned Result is {0}", sResult);
            
//For Debug
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();

 (4)测试结果:
   启动宿主程序,然后启动客户端程序,当我们提供了有效的Windows域账号和密码的时候,稍作等待,客户端成功调用服务,宿主打印的消息。如图:

【5】总结
     Windows Communication Foundation (WCF) 服务和客户端。WCF安全机制都是依赖现有的安全体系和框架来完成的。Windows Server 2003系统使用域名服务(DNS)查询定位最近的可用域控制器。该域控制器则会在用户登录期间对该用户起首选KDC的作用。如果首选KDC失效,则Windows 2003 Server系统将确定由另一个KDC提供验证。
   (1)服务器不需要 X.509 证书,这里是借助DC来实现服务器身份证明。
   (2)绑定设置为 Message 安全模式,客户端凭据类型设置为 Windows
   (3)初始协商完毕以后,建立共享安全上下文,这里使用商定的加密算法对SOAP消息进行加密和签名。
   (4)这里的互操作行仅仅限制在WCF平台上的客户端和服务端的安全验证。
   (5)这里客户端调用WCF服务以前要提供提供了有效的Windows域账户和密码,和Transport安全模式的Windows方式有差别,这里使用的是消息安全,客户端身份验证使用的是Windows域服务器
   (6)参考代码:/Files/frank_xl/7.3.WCFServiceSecurityDemoFrankXuLei_Message_Windows_NetTcpBinding.rar
参考文章:
1.WCF分布式安全开发实践(4):传输安全模式之Windows身份验证:Transport_Windows_NetTcpBinding
2.WCF分布式安全开发实践(7):消息安全模式之匿名客户端:Message_None_WSHttpBinding
3.http://social.microsoft.com/Forums/zh-CN/wcfzhchs/thread/e1aa7bea-90d8-41e6-b91b-7addba44f8e3
4.WSE3.0构建Web服务安全(2):非对称加密、公钥、密钥、证书、签名的区别和联系以及X.509 证书的获得和管理,具体5.http://msdn.microsoft.com/en-us/library/ms729709.aspx


老徐的博客
【作      者】:Frank Xu Lei
【地      址】:http://www.cnblogs.com/frank_xl/
【中文论坛】:微软WCF中文技术论坛
【英文论坛】:微软WCF英文技术论坛

原创粉丝点击