【问题标题】:How to secure a WCF service by session?如何通过会话保护 WCF 服务?
【发布时间】:2011-04-24 11:32:24
【问题描述】:
我的坦白:我是 WCF 的新手,我读过一些关于它的东西,但我接触它还不到一周。
我想通过会话来保护我的 WCF,这和网页一样,一开始客户端需要识别自己,但是当它通过身份验证时,WCF 服务会信任它,直到会话超时。
因为服务是时间关键的,所以安全机制应该尽可能的少。
如上所述,我没有使用 WCF 的经验,所以我不知道我的想法是否可以实现,以及 WCF 广泛使用哪些机制。
非常感谢。
【问题讨论】:
标签:
wcf
web-services
security
authentication
【解决方案1】:
最佳做法是使用无会话服务,因为引入会话会导致其他复杂性。
在您的情况下,可以使用 WS-SecureConversation、WS-Trust 等提供的安全会话(安全上下文)来保护 SOAP 服务。使用任何类型的 WCF 会话时,您必须重用相同的服务代理实例。会话存在于特定代理和服务实例之间。一旦其中任何一个死掉或连接出现错误,会话就会消失,您必须打开一个新的代理。
使用安全对话时,您需要将所需凭据填写到服务代理中并运行您的通信。代理会将这些凭据发送到服务,服务将创建一个安全令牌,用于后续通信。这个初始握手有一些额外的成本。以下通信由令牌保护。 WCF 将此与消息级加密和签名一起使用,这需要额外的成本。您可以关闭某些消息部分的加密和签名,但至少必须对与身份验证相关的信息进行加密。
此类服务的基本配置如下所示:
<bindings>
<wsHttpBinding>
<binding name="secured">
<security mode="Message">
<message clientCredentialType="UserName" estabilishSecurityContext="true"
negotiateServiceCredentials="false" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="securedService">
...
<serviceCredentials>
<!-- Allows configuring how user name and password will be validated -->
<userNameAuthentication ... />
<!-- Message security with user name and password credentials requires service certificate -->
<serviceCertificate ... />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="..." behaviorConfiguration="securedService">
<endpoint address="" contract="..." binding="wsHttpBinding"
bindingConfiguration="secured" />
</service>
</services>
这是在 WCF 中执行此操作的标准方法,安全性集成到 WCF 安全管道中。任何其他方法主要是绕过 WCF 安全管道或修改安全管道 - 这两种方法都需要大量的自定义开发。