通过绑定元素看各种绑定对消息保护的实现

为了演示方便,我写了如下一个针对Binding类型的扩展方法ListAllBindingElements,该方法会将绑定所有的绑定元素的类型打印出来。接下来,我们就利用这个扩展方法应用了那些常见的绑定,看看最终决定安全传输的是哪些绑定元素。

   1: public static class BindingExtension

   2: {

   3:     public static void ListAllBindingElements(this Binding binding)

   4:     {

   5:         int i = 0;

   6:         foreach (var bindingElement in binding.CreateBindingElements())

   7:         {

   8:             Console.WriteLine("\t{0}.{1}", ++i, bindingElement.GetType().FullName);

   9:         }

  10:     }

  11: }

一、BasicHttpBinding

我们先来看看对于三种典型安全模式(Transport、Message和Mixed)下的BasicHttpBinding具体由哪些绑定元素构成,为了我编写了如下的程序。

   1: BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);

   2: Console.WriteLine("Transport:");

   3: binding.ListAllBindingElements();

   4: 

   5: binding = new BasicHttpBinding(BasicHttpSecurityMode.Message);

   6: binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate;

   7: Console.WriteLine("Message:");

   8: binding.ListAllBindingElements();

   9: 

  10: binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);

  11: Console.WriteLine("Mixed:");

  12: binding.ListAllBindingElements();

输出结果:

   1: Transport:

   2:         1.System.ServiceModel.Channels.TextMessageEncodingBindingElement

   3:         2.System.ServiceModel.Channels.HttpsTransportBindingElement

   4: Message:

   5:         1.System.ServiceModel.Channels.AsymmetricSecurityBindingElement

   6:         2.System.ServiceModel.Channels.TextMessageEncodingBindingElement

   7:         3.System.ServiceModel.Channels.HttpTransportBindingElement

   8: Mixed:

   9:         1.System.ServiceModel.Channels.TransportSecurityBindingElement

  10:         2.System.ServiceModel.Channels.TextMessageEncodingBindingElement

  11:         3.System.ServiceModel.Channels.HttpsTransportBindingElement

我们来具体分析一下最终在不同安全模式下输出的绑定元素列表。对于Mixe安全模式下对服务的验证、消息签名和加密都是基于Transport安全,Message安全仅仅用于对客户端的认证。所以对于Transport和Mixed模式,消息保护都是通过HttpsTransportBindingElement 来实现。从名称就可以看出来,这是一个基于HTTPS的传输绑定元素,这也再次印证了BasicHttpBinding通过HTTPS实现Transport安全模式的说法。asp.net利用.net控件实现下拉导航菜单的制作

对于Message安全模式的三个绑定元素中,很明显和安全传输相关的是AsymmetricSecurityBindingElement 。从名称我们就知道,该绑定元素通过非对称加密 的 方式提供签名和加密的实现。具体来说,对于请求消息来说,发送方使用自己的私钥对消息进行签名,使用接收方的公钥对消息进行加密。接收方采用发送方的公钥 验证签名,用自己的私钥对消息进行解密。这也是为什么在选择了Message安全模式的情况下,基于用户名/密码的客户端凭证不被支持的真正原因。

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值