WCF ProtectionLevel

Alb*_*ano 1 c# wcf

如果我将ProtectionLevel应用为服务合同的属性:

  [ServiceContract(ProtectionLevel=ProtectionLevel.EncryptAndSign)]
  public interface IService
   {
    [OperationContract]
    string GetData1(long token);

    [OperationContract]
    string GetData2(long token);

    [OperationContract]
    string GetData3(long token);

   }
Run Code Online (Sandbox Code Playgroud)

它会应用于所有方法吗?我的意思是,我的所有方法都会签名加密?

在每个方法上使用MessageContract属性的区别是什么?(无论粒度如何,在这种情况下,我的目标都是安全的所有方法)

我知道使用MessageContract将限制返回[MessageContract]标记的类,并使用[MessageContract]类作为参数.我可以使用基本类型获得相同的结果,并使用接口级别的属性加密我的方法的所有参数和返回吗?

我打算使用wsHttpBinding.

der*_*era 7

当ProtectionLevel在接口级别设置时,它适用于所有OperationContracts和MessageContracts.

层次结构如下.同一级别的属性是同级.

  1. ServiceContractAttribute的

  2. OperationContractAttribute

  3. MessageContractAttribute,FaultContractAttribute

  4. MessageHeaderAttribute,MessageBodyMemberAttribute

在最顶层设置ProtectionLevel可为其下方的所有设置级别.如果将ProtectionLevel设置为较低级别的其他值,则层次结构中该级别以下的所有值都将重置为新级别

在每个MessageContract级别应用ProtectionLevel用于粒度控制

public class Record
{
   [MessageHeader(ProtectionLevel=None)] public int recordID;
   [MessageHeader(ProtectionLevel=EncryptAndSign)] public string SSN;
   [MessageBodyMember(ProtectionLevel=None)] public string comments;
   [MessageBodyMember(ProtectionLevel=EncryptAndSign)] public string history;
}
Run Code Online (Sandbox Code Playgroud)

对于邮件标题,将为每个标题单独确定保护级别.

对于邮件正文部分,正文的保护级别由所有正文部分的最高ProtectionLevel属性设置确定.

出于以下原因,建议使用MessageContracts

使用MessageContracts的优点

  • 它对基于SOAP的通信特别有用
  • 控制SOAP消息的结构控制其内容.
  • 控制消息或消息部分级别的安全性问题
  • 互操作性(即.net或java /客户端或服务之间的通信)

    [MessageContract]
    public class Record
    {
      [MessageHeader(Name="ID")] public int personID;
      [MessageBodyMember(Order=1)] public string comments;
    }
    
    Run Code Online (Sandbox Code Playgroud)

要使安全功能起作用,您必须在配置中或通过代码正确配置绑定和行为.

下面显示了使用wsHttpBinding的典型消息安全绑定

<bindings>
  <wsHttpBinding>
    <binding name="wsHttpBindingMessageSecurity">
      <security mode="message">
      </security>
    </binding>
  </wsHttpBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)

以上内容将根据您的安全要求而变化.

没有消息合同

您可以在没有消息合同的情况下配置WCF服务.没有消息合同,实现安全性将正常工作.

下面是一个典型的例子

Service Contract,它有一个返回字符串的方法(原始数据类型)

[ServiceContract(ProtectionLevel = ProtectionLevel.EncryptAndSign)]
public interface IService
{
    [OperationContract(IsOneWay = false)]
    string Register();
}
Run Code Online (Sandbox Code Playgroud)

这是绑定

<wsHttpBinding>
    <binding name="wsHttpBindingConfiguration" receiveTimeout="00:10:00"  sendTimeout="10.00:00:00" maxBufferPoolSize="1073741824" maxReceivedMessageSize="1073741824">
      <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
      <security>
        <message clientCredentialType="Windows"/>
      </security>
    </binding>
  </wsHttpBinding>
Run Code Online (Sandbox Code Playgroud)

这是加密响应(仅为简洁起见)

<s:Body u:Id="_0">
<e:EncryptedData Id="_1" Type="http://www.w3.org/2001/04/xmlenc#Content" xmlns:e="http://www.w3.org/2001/04/xmlenc#">
  <e:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc"/>
  <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
    <o:SecurityTokenReference xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
      <o:Reference ValueType="http://schemas.xmlsoap.org/ws/2005/02/sc/dk" URI="#uuid-fab89344-49bb-4b84-a7ea-02bad97b9142-6"/>
    </o:SecurityTokenReference>
  </KeyInfo>
  <e:CipherData>
    <e:CipherValue>BFlxwcK/QcXFlGUWNoE+LAOSizI1BEFKHlpDdHvby9PRwPTQFRztn+1pWmz8S0UgKzM/Puqud3N0G1tb/xcLsdNyIqgvQ68UjG+g5LGyqlbUEHa4+LaCWvW7ADN3eqoP+y1mhrN91ehIPpgYclrFHcIv/UDVCB+LLG4iMMikGqY=</e:CipherValue>
  </e:CipherData>
</e:EncryptedData>
Run Code Online (Sandbox Code Playgroud)

因此,为了实现安全性,没有必要使用消息合同

希望这可以帮助.