WCF保护级别
本文关键字:保护 WCF | 更新日期: 2023-09-27 17:57:45
如果我将ProtectionLevel应用为服务合同的属性:
[ServiceContract(ProtectionLevel=ProtectionLevel.EncryptAndSign)]
public interface IService
{
[OperationContract]
string GetData1(long token);
[OperationContract]
string GetData2(long token);
[OperationContract]
string GetData3(long token);
}
它会应用于所有的方法吗?我的意思是,我所有的方法都会被加密签名?
在每个方法上使用MessageContract属性的区别是什么?(与粒度无关,在这种情况下,我的目标是确保所有方法的安全)
我知道使用MessageContract会有返回[MessageContract]标记类的限制,也会使用[MessageContract]class作为参数。我可以使用基元类型并使用接口级别的属性加密方法的所有参数和返回值来获得相同的结果吗?
我计划使用wsHttpBinding。
在接口级别设置ProtectionLevel时,它将应用于所有OperationContracts和MessageContracts。
层次结构如下。同一级别的属性是对等的。
-
ServiceContractAttribute
-
OperationContractAttribute
-
MessageContractAttribute,FaultContractAttribute
-
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;
}
对于消息头,每个头的保护级别是单独确定的。
对于消息正文部分,正文的保护级别由所有正文部分的最高ProtectionLevel属性设置确定。
出于以下原因,建议使用MessageContracts
使用MessageContracts的优势
- 它对于基于SOAP的通信特别有用
- 控制SOAP消息的结构控制其内容
- 控制消息或消息部分级别的安全问题
互操作性(比如.net或java/客户端之间的通信或服务)
[MessageContract] public class Record { [MessageHeader(Name="ID")] public int personID; [MessageBodyMember(Order=1)] public string comments; }
为了使安全功能发挥作用,您必须在配置中或通过代码正确地配置绑定和行为。
下面显示了使用wsHttpBinding 的典型消息安全绑定
<bindings>
<wsHttpBinding>
<binding name="wsHttpBindingMessageSecurity">
<security mode="message">
</security>
</binding>
</wsHttpBinding>
</bindings>
以上内容将根据您的安全要求进行更改。
无消息合约
您可以在不使用消息协定的情况下配置WCF服务。如果没有消息契约,实现安全性将很好。
以下是的典型示例
具有返回字符串(原始数据类型)的方法的服务合同
[ServiceContract(ProtectionLevel = ProtectionLevel.EncryptAndSign)]
public interface IService
{
[OperationContract(IsOneWay = false)]
string Register();
}
这是绑定
<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>
这是加密的响应(为简洁起见,仅为正文)
<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>
因此,为了实现安全性,没有必要使用消息合约
希望这能有所帮助。