WCF BasicHttpBinding 安全解析(2)BasicHttpBinding安全项

简介:

想对BasicHttpBinding的安全性做比较全面的了解,最好的办法还是从它的安全属性看起。下面展示的所有源代码通过反编译获得,这里我们根据需要选取关键的代码来分析,先看代码清单11-73。

代码清单11-73 BasicHttpBinding定义

   1:  public class BasicHttpBinding : Binding, IBindingRuntimePreferences
   2:   
   3:          {
   4:   
   5:  private HttpTransportBindingElement httpTransport;
   6:   
   7:  private HttpsTransportBindingElement httpsTransport;
   8:   
   9:  private TextMessageEncodingBindingElement textEncoding;
  10:   
  11:  private MtomMessageEncodingBindingElement mtomEncoding;
  12:   
  13:  private BasicHttpSecurity security;
  14:   
  15:  public BasicHttpBinding(BasicHttpSecurityMode securityMode)
  16:   
  17:  {
  18:   
  19:  this.security = new BasicHttpSecurity();
  20:   
  21:  this.security.Mode = securityMode;
  22:   
  23:  }
  24:   
  25:  private BasicHttpBinding(BasicHttpSecurity security)
  26:   
  27:  {
  28:   
  29:  this.security = new BasicHttpSecurity();
  30:   
  31:  this.security = security;
  32:   
  33:  }
  34:   
  35:          }
  36:   

从代码清单11-73中,我们可以看到关键的对象为BasicHttpSecurity,在构造函数中BasicHttpBinding类对其初始化并设置securityMode。下面我们看BasicHttpSecurity的定义。

代码清单11-74 BasicHttpSecurity定义

   1:  public sealed class BasicHttpSecurity
   2:   
   3:      {
   4:   
   5:  internal const BasicHttpSecurityMode DefaultMode = BasicHttpSecurityMode.None;
   6:   
   7:  private BasicHttpSecurityMode mode;
   8:   
   9:  private HttpTransportSecurity transportSecurity;
  10:   
  11:  private BasicHttpMessageSecurity messageSecurity;
  12:   
  13:  public BasicHttpSecurityMode Mode
  14:   
  15:          {
  16:   
  17:              [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  18:   
  19:  get
  20:   
  21:              {
  22:   
  23:  return this.mode;
  24:   
  25:              }
  26:   
  27:  set
  28:   
  29:              {
  30:   
  31:  if (!BasicHttpSecurityModeHelper.IsDefined(value))
  32:   
  33:                  {
  34:   
  35:  throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
  36:   
  37:                  }
  38:   
  39:  this.mode = value;
  40:   
  41:              }
  42:   
  43:          }
  44:   
  45:  public HttpTransportSecurity Transport
  46:   
  47:          {
  48:   
  49:              [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  50:   
  51:  get
  52:   
  53:              {
  54:   
  55:  return this.transportSecurity;
  56:   
  57:              }
  58:   
  59:  set
  60:   
  61:              {
  62:   
  63:  this.transportSecurity = ((value == null) ? new HttpTransportSecurity() : value);
  64:   
  65:              }
  66:   
  67:          }
  68:   
  69:  public BasicHttpMessageSecurity Message
  70:   
  71:          {
  72:   
  73:              [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  74:   
  75:  get
  76:   
  77:              {
  78:   
  79:  return this.messageSecurity;
  80:   
  81:              }
  82:   
  83:  set
  84:   
  85:              {
  86:   
  87:  this.messageSecurity = ((value == null) ? new BasicHttpMessageSecurity() : value);
  88:   
  89:              }
  90:   
  91:          }
  92:   
  93:  public BasicHttpSecurity()
  94:   
  95:              : this(BasicHttpSecurityMode.None, new HttpTransportSecurity(), new BasicHttpMessageSecurity())
  96:   
  97:          {
  98:   
  99:          }
 100:   
 101:  private BasicHttpSecurity(BasicHttpSecurityMode mode, HttpTransportSecurity transportSecurity, BasicHttpMessageSecurity messageSecurity)
 102:   
 103:          {
 104:   
 105:  this.Mode = mode;
 106:   
 107:  this.transportSecurity = ((transportSecurity == null) ? new HttpTransportSecurity() : transportSecurity);
 108:   
 109:  this.messageSecurity = ((messageSecurity == null) ? new BasicHttpMessageSecurity() : messageSecurity);
 110:   
 111:          }
 112:   
 113:      }
 114:   

根据代码清单11-74,我们对BasicHttpSecurity做简要的分析。首先看第一个属性——Mode。Mode是BasicHttpSecurityMode枚举值之一,表示安全类型,默认值为None。BasicHttpSecurityMode枚举共提供5种选择:

1) None:OAP 消息在传输过程中并不安全。 这是默认行为。

2) Transport:使用 HTTPS 提供安全性。 此服务必须使用 SSL 证书进行配置。 SOAP 消息是用 HTTPS 作为一个整体进行保护的。 客户端使用服务的 SSL 证书对服务进行身份验证。 通过 ClientCredentialType 可对客户端身份验证进行控制。

3) Message:使用 SOAP 消息安全提供安全性。对于BasicHttpBinding,系统要求向客户端单独提供服务器证书。此绑定的有效客户端凭据类型为UserName和Certificate。

4) TransportWithMessageCredential:完整性、保密性和服务器身份验证均由 HTTPS 提供。 此服务必须使用证书进行配置。 客户端身份验证采用SOAP消息安全方式提供。 如果要使用用户名或证书凭据对用户进行身份验证,并且存在用于保护消息传输的现有HTTPS部署,则适用此模式。

5) TransportCredentialOnly:此模式并不提供消息的完整性和保密性, 而是仅提供基于HTTP 的客户端身份验证。 使用此模式时一定要小心。 在通过其他方式(如IPSec)提供传输安全并且 基础结构只提供客户端身份验证的环境中,应该使用此模式。

可使用如代码清单11-75所示的配置方式配置安全模式。

代码清单11-75 配置安全模式

   1:  <basicHttpBinding>
   2:   
   3:          <binding name="basicBidingConf">
   4:   
   5:    <security mode="None">
   6:   
   7:            </security>
   8:   
   9:          </binding>
  10:   
  11:        </basicHttpBinding>

在代码清单11-74中我们看BasicHttpSecurity的第二个属性——Transport,该属性是HttpTransportSecurity实例。HttpTransportSecurity 类定义如代码清单11-75。

代码清单11-75 HttpTransportSecurity 类定义

   1:  public sealed class HttpTransportSecurity
   2:   
   3:  {
   4:   
   5:  internal const HttpClientCredentialType DefaultClientCredentialType = HttpClientCredentialType.None;
   6:   
   7:  internal const HttpProxyCredentialType DefaultProxyCredentialType = HttpProxyCredentialType.None;
   8:   
   9:  internal const string DefaultRealm = "";
  10:   
  11:  private HttpClientCredentialType clientCredentialType;
  12:   
  13:  private HttpProxyCredentialType proxyCredentialType;
  14:   
  15:  private string realm;
  16:   
  17:  private ExtendedProtectionPolicy extendedProtectionPolicy;
  18:   
  19:  public HttpClientCredentialType ClientCredentialType;
  20:   
  21:  public HttpProxyCredentialType ProxyCredentialType;
  22:   
  23:  public string Realm;
  24:   
  25:  public ExtendedProtectionPolicy ExtendedProtectionPolicy;
  26:   
  27:  public HttpTransportSecurity()
  28:   
  29:      {
  30:   
  31:  this.clientCredentialType = HttpClientCredentialType.None;
  32:   
  33:  this.proxyCredentialType = HttpProxyCredentialType.None;
  34:   
  35:  this.realm = "";
  36:   
  37:  this.extendedProtectionPolicy = ChannelBindingUtility.DefaultPolicy;
  38:   
  39:      }
  40:   
  41:  }
  42:   

从代码清单11-75中我们知道HttpTransportSecurity 类包含四个属性:

1) ClientCredentialType属性。获取或设置要用于身份验证的客户端凭据的类型。默认值为HttpClientCredentialType.None。

2) ExtendedProtectionPolicy。获取或设置扩展保护策略,默认值为ChannelBindingUtility.DefaultPolicy。

3) ProxyCredentialType。获取或设置要用于针对代理进行身份验证的客户端凭据的类型。默认值为HttpProxyCredentialType.None。

4) Realm。获取或设置摘要式或基本身份验证的身份验证领域,默认值为空。

BasicHttpSecurity 类的第三个属性为BasicHttpMessageSecurity类,用来配置BasicHttpBinding的消息安全。该类定义如代码清单11-76所示。

代码清单11-76 BasicHttpMessageSecurity类定义

   1:  public sealed class BasicHttpMessageSecurity
   2:   
   3:  {internal const BasicHttpMessageCredentialType DefaultClientCredentialType=BasicHttpMessageCredentialType.UserName;
   4:   
   5:  private BasicHttpMessageCredentialType clientCredentialType;
   6:   
   7:  private SecurityAlgorithmSuite algorithmSuite;
   8:   
   9:  public BasicHttpMessageCredentialType ClientCredentialType
  10:   
  11:  {get{return this.clientCredentialType;}
  12:   
  13:  set{
  14:   
  15:  if (!BasicHttpMessageCredentialTypeHelper.IsDefined(value))
  16:   
  17:  {
  18:   
  19:  throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));}
  20:   
  21:  this.clientCredentialType = value;}
  22:   
  23:  }
  24:   
  25:  public SecurityAlgorithmSuite AlgorithmSuite
  26:   
  27:  {
  28:   
  29:  get{return this.algorithmSuite;}
  30:   
  31:  set
  32:   
  33:  {if (value == null)
  34:   
  35:  {
  36:   
  37:  throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");}
  38:   
  39:  this.algorithmSuite = value;}
  40:   
  41:  }
  42:   
  43:  public BasicHttpMessageSecurity()
  44:   
  45:  {
  46:   
  47:  this.clientCredentialType = BasicHttpMessageCredentialType.UserName;
  48:   
  49:  this.algorithmSuite = SecurityAlgorithmSuite.Default;
  50:   
  51:  }}
  52:   

从代码清单11-76中我们可以看到BasicHttpMessageSecurity类包含两个属性:

1) AlgorithmSuite。指定要与 BasicHttpMessageSecurity 一起使用的算法组。

2) ClientCredentialType。发送安全消息指定客户端用以进行身份验证的凭据的类型。

那么在配置文件中如何配置BasicHttpSecurity呢?代码清单11-77给出了一般配置选项。

代码清单11-77 配置BasicHttpSecurity

   1:  <basicHttpBinding>
   2:   
   3:  <binding 
   4:   
   5:  transferMode="Buffered/Streamed/StreamedRequest/StreamedResponse"
   6:   
   7:  useDefaultWebProxy="Boolean"
   8:   
   9:  <security mode="None/Transport/Message/TransportWithMessageCredential/TransportCredentialOnly">
  10:   
  11:  <transport clientCredentialType="None/Basic/Digest/Ntlm/Windows/Certificate" proxyCredentialType="None/Basic/Digest/Ntlm/Windows"
  12:   
  13:  realm="string" />
  14:   
  15:  <message algorithmSuite="Basic128/Basic192/Basic256/Basic128Rsa15/Basic256Rsa15/TripleDes/TripleDesRsa15/Basic128Sha256/Basic192Sha256/TripleDesSha256/Basic128Sha256Rsa15/Basic192Sha256Rsa15/Basic256Sha256Rsa15/TripleDesSha256Rsa15"
  16:   
  17:  clientCredentialType="UserName/Certificate"/>
  18:   
  19:  </security>
  20:   
  21:  <readerQuotas maxDepth="Integer" 
  22:   
  23:  maxStringContentLength="Integer"
  24:   
  25:  maxByteArrayContentLength="Integer"
  26:   
  27:  maxBytesPerRead="Integer"
  28:   
  29:  maxNameTableCharCount="Integer" />
  30:   
  31:  </binding>
  32:   
  33:  </basicHttpBinding>
  34:   

代码清单11-77所示的配置节中各项的含义读者可以参考BasicHttpSecurity 类的个属性进行解读,这里就不再重复了。下面我们通过实例继续探讨BasicHttpBinding的更多安全特性。


本文转自悬魂博客园博客,原文链接:http://www.cnblogs.com/xuanhun/archive/2011/06/27/2091302.html,如需转载请自行联系原作者

相关文章
|
10月前
|
机器学习/深度学习 安全 大数据
揭秘!企业级大模型如何安全高效私有化部署?全面解析最佳实践,助你打造智能业务新引擎!
【10月更文挑战第24天】本文详细探讨了企业级大模型私有化部署的最佳实践,涵盖数据隐私与安全、定制化配置、部署流程、性能优化及安全措施。通过私有化部署,企业能够完全控制数据,确保敏感信息的安全,同时根据自身需求进行优化,提升计算性能和处理效率。示例代码展示了如何利用Python和TensorFlow进行文本分类任务的模型训练。
624 6
|
9月前
|
域名解析 负载均衡 安全
DNS技术标准趋势和安全研究
本文探讨了互联网域名基础设施的结构性安全风险,由清华大学段教授团队多年研究总结。文章指出,DNS系统的安全性不仅受代码实现影响,更源于其设计、实现、运营及治理中的固有缺陷。主要风险包括协议设计缺陷(如明文传输)、生态演进隐患(如单点故障增加)和薄弱的信任关系(如威胁情报被操纵)。团队通过多项研究揭示了这些深层次问题,并呼吁构建更加可信的DNS基础设施,以保障全球互联网的安全稳定运行。
|
10月前
|
机器学习/深度学习 人工智能 安全
TPAMI:安全强化学习方法、理论与应用综述,慕工大、同济、伯克利等深度解析
【10月更文挑战第27天】强化学习(RL)在实际应用中展现出巨大潜力,但其安全性问题日益凸显。为此,安全强化学习(SRL)应运而生。近日,来自慕尼黑工业大学、同济大学和加州大学伯克利分校的研究人员在《IEEE模式分析与机器智能汇刊》上发表了一篇综述论文,系统介绍了SRL的方法、理论和应用。SRL主要面临安全性定义模糊、探索与利用平衡以及鲁棒性与可靠性等挑战。研究人员提出了基于约束、基于风险和基于监督学习等多种方法来应对这些挑战。
277 2
|
11月前
|
安全 Java 编译器
Java 泛型深入解析:类型安全与灵活性的平衡
Java 泛型通过参数化类型实现了代码重用和类型安全,提升了代码的可读性和灵活性。本文深入探讨了泛型的基本原理、常见用法及局限性,包括泛型类、方法和接口的使用,以及上界和下界通配符等高级特性。通过理解和运用这些技巧,开发者可以编写更健壮和通用的代码。
208 1
|
11月前
|
安全 网络安全 Android开发
深度解析:利用Universal Links与Android App Links实现无缝网页至应用跳转的安全考量
【10月更文挑战第2天】在移动互联网时代,用户经常需要从网页无缝跳转到移动应用中。这种跳转不仅需要提供流畅的用户体验,还要确保安全性。本文将深入探讨如何利用Universal Links(仅限于iOS)和Android App Links技术实现这一目标,并分析其安全性。
1333 0
|
区块链 C# 存储
链动未来:WPF与区块链的创新融合——从智能合约到去中心化应用,全方位解析开发安全可靠DApp的最佳路径
【8月更文挑战第31天】本文以问答形式详细介绍了区块链技术的特点及其在Windows Presentation Foundation(WPF)中的集成方法。通过示例代码展示了如何选择合适的区块链平台、创建智能合约,并在WPF应用中与其交互,实现安全可靠的消息存储和检索功能。希望这能为WPF开发者提供区块链技术应用的参考与灵感。
178 0
|
安全 开发者 数据安全/隐私保护
Xamarin 的安全性考虑与最佳实践:从数据加密到网络防护,全面解析构建安全移动应用的六大核心技术要点与实战代码示例
【8月更文挑战第31天】Xamarin 的安全性考虑与最佳实践对于构建安全可靠的跨平台移动应用至关重要。本文探讨了 Xamarin 开发中的关键安全因素,如数据加密、网络通信安全、权限管理等,并提供了 AES 加密算法的代码示例。
137 0
|
6月前
|
算法 测试技术 C语言
深入理解HTTP/2:nghttp2库源码解析及客户端实现示例
通过解析nghttp2库的源码和实现一个简单的HTTP/2客户端示例,本文详细介绍了HTTP/2的关键特性和nghttp2的核心实现。了解这些内容可以帮助开发者更好地理解HTTP/2协议,提高Web应用的性能和用户体验。对于实际开发中的应用,可以根据需要进一步优化和扩展代码,以满足具体需求。
570 29
|
6月前
|
前端开发 数据安全/隐私保护 CDN
二次元聚合短视频解析去水印系统源码
二次元聚合短视频解析去水印系统源码
174 4
|
6月前
|
JavaScript 算法 前端开发
JS数组操作方法全景图,全网最全构建完整知识网络!js数组操作方法全集(实现筛选转换、随机排序洗牌算法、复杂数据处理统计等情景详解,附大量源码和易错点解析)
这些方法提供了对数组的全面操作,包括搜索、遍历、转换和聚合等。通过分为原地操作方法、非原地操作方法和其他方法便于您理解和记忆,并熟悉他们各自的使用方法与使用范围。详细的案例与进阶使用,方便您理解数组操作的底层原理。链式调用的几个案例,让您玩转数组操作。 只有锻炼思维才能可持续地解决问题,只有思维才是真正值得学习和分享的核心要素。如果这篇博客能给您带来一点帮助,麻烦您点个赞支持一下,还可以收藏起来以备不时之需,有疑问和错误欢迎在评论区指出~

推荐镜像

更多
  • DNS