金喜正规买球

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

原创|其它|编辑:郝浩|2009-08-26 11:36:41.000|阅读 1164 次

概述:今天继续WCF分布式安全开发实践(9):消息安全模式之Windows身份验证:Message_Windows_NetTcpBinding。本文介绍的内容主要是:主要是消息安全模式的Windows身份验证方式,基于NetTcpBinding绑定协议的实现过程。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

  今天继续WCF分布式安全开发实践(9):消息安全模式之Windows身份验证:Message_Windows_NetTcpBinding。本文介绍的内容主要是:主要是消息安全模式的Windows身份验证方式,基于NetTcpBinding绑定协议的实现过程。主要内容:基本概念,服务端配置、客户端配置、总结。   Windows 域验证的原理在 有简单介绍。  这里就不在重复。
【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 = "//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">
 &nbsp;      
<security mode="Message">
     &nbsp;    
<transport clientCredentialType="None"  protectionLevel="None"/>
  &nbsp;       
<message clientCredentialType="Windows"/>
 &nbsp;      
</security>
      
</binding>
    
</netTcpBinding>
    
</bindings>


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

 

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

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

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

 

 

 

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

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

////HTTP WSHttpBinding_IWCFService1
     ;      &nbsp;WCFClient.ClientProxy.WCFServiceClient wcfServiceProxy = new WCFClient.ClientProxy.WCFServiceClient("NetTcpBinding_IWCFService");
        &nbsp;   
//通过代理调用SayHello服务
           ;&nbsp;string sName = "Frank Xu Lei Messaage Windows NetTcpBinding";
          &nbsp; 
string sResult = string.Empty;
    &nbsp;       wcfServiceProxy.ClientCredentials.Windows.ClientCredential.UserName 
= "FrankXuLei";
  &nbsp;   &nbsp;     wcfServiceProxy.ClientCredentials.Windows.ClientCredential.Password 
= "00000000";
            wcfServiceProxy.ClientCredentials.Windows.ClientCredential.Domain 
= "frankxulei.com";
      &nbsp;&nbsp;    sResult 
= wcfServiceProxy.SayHello(sName);
            Console.WriteLine(
"Returned Result is {0}", sResult);
      &nbsp;     
//For Debug
       &nbsp; ;   Console.WriteLine("Press any key to exit");
           &nbsp;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)参考代码:


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@fc6vip.cn

文章转载自:博客园

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP