如果我将 ProtectionLevel 应用为服务契约(Contract)的属性:
[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] 类作为参数。 我能否使用原始类型并使用接口(interface)级别的属性加密所有参数并返回我的方法来获得相同的结果?
我打算使用 wsHttpBinding。
最佳答案
当在接口(interface)级别设置 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;
}
对于邮件 header ,保护级别是为每个 header 单独确定的。
对于消息正文部分,正文的保护级别由所有正文部分的最高 ProtectionLevel 属性设置决定。
出于以下原因,建议使用 MessageContracts
使用消息合约的优势
- 它对基于 SOAP 的通信特别有用
- 控制 SOAP 消息的结构 控制其内容。
- 在消息或消息部分级别控制安全问题
互操作性(例如 .net 或 java/客户端之间的通信或 服务)
[MessageContract] public class Record { [MessageHeader(Name="ID")] public int personID; [MessageBodyMember(Order=1)] public string comments; }
要使安全功能发挥作用,您必须在配置中或通过代码正确配置绑定(bind)和行为。
下面显示了使用 wsHttpBinding 的典型消息安全绑定(bind)
<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();
}
这是绑定(bind)
<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>
所以为了实现安全性,没有必要有消息契约
希望这对您有所帮助。
关于c# - WCF 保护级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22998847/