【问题标题】:WCF sessions with HTTPS使用 HTTPS 的 WCF 会话
【发布时间】:2010-09-30 11:42:53
【问题描述】:

我不知道如何在使用 HTTPS 时为我的 WCF 服务启用每会话实例。 (我不是 ASP.NET 专家,但如果可能的话,我不想使用 ASP.NET 会话状态。)我使用的是 .NET Framework 3.0。

我得出了以下矛盾,希望有人能告诉我逻辑上的缺陷在哪里。

1) 由于客户端要求,该服务必须托管在 IIS 6 上。

2)服务需要维护调用之间的状态,包括SqlConnection和SqlTransaction实例(丑陋但由于项目限制是必要的)。

3) 因此我需要使用 wsHttpBinding。

4) 服务需要能够从 HttpContext.Current.User.Identity 访问用户身份验证信息(例如,在 IIS 中使用 Windows 安全性)。

5) 因此需要 HTTPS。

6) 因此必须在绑定上配置传输级安全性。

7) 将服务配置为需要会话意味着我必须将 wsHttpBinding 配置为使用可靠会话。

8) 这需要在绑定上配置消息级安全性。

即(6) 和 (8) 互斥。

似乎使用 WCF 会话需要我使用消息级安全性,这使我无法使用 HTTPS。

我错过了什么?

【问题讨论】:

  • 您是否遇到了此配置的特定错误,或者您是否正在检查此配置是否可行?
  • 我想知道配置是否可行:即带有 HTTPS 的 WCF 会话。谢谢。

标签: c# wcf https iis-6


【解决方案1】:

3) TruewsHttpBindingwsDualHttpBinding 是唯一支持会话的 HTTP 绑定

5) False,为了对服务调用者进行身份验证,您不一定需要任何传输级别的安全性(例如 SSL/HTTPS)。唯一的要求是将 IIS 配置为为虚拟目录启用 集成 Windows 身份验证。然后在 WCF 中,您可以通过三种方式启用此方案:

a) 在带有 Windows 凭据 (HTTPS) 的 wsHttpBinding 上使用传输级安全性

<system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding name="SecurityEnabledWsHttp">
                <security mode="Transport">
                    <transport clientCredentialType="Windows" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
</system.serviceModel>

b) 在带有 Windows 凭据 (HTTP) 的 wsHttpBinding 上使用消息级安全性

<system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding name="SecurityEnabledWsHttp">
                <security mode="Message">
                    <message clientCredentialType="Windows" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
</system.serviceModel>

c) 在 ASP.NET 兼容模式 下运行您的服务并在 ASP.NET (HTTP) 中启用 Windows 身份验证

<system.web>
    <authentication mode="Windows" />
</system.web>

请注意,在 ab 中,您将通过以下方式从服务中访问调用者的身份:

OperationContext.Current.ServiceSecurityContext.WindowsIdentity

6) True,必须在 wsHttpBinding 上启用传输级安全性才能使用 HTTPS

7) FalseReliable Sessions 是用于 WCF 会话的 Reliable Messaging 的特定实现。 Reliable Messaging 是一种 WS-* 标准规范,旨在保证在不可靠的网络上传递消息。您可以在没有可靠消息传递的情况下使用 WCF 会话,反之亦然。使用此属性在服务合同上启用会话:

[ServiceContract(SessionMode=SessionMode.Required)]
public interface IMyService {
    // ...
}

还请记住,为了维护服务调用之间的状态,您必须明确地在服务合同实现上启用适当的实例模式:

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
public class MyService : IMyService {
    // ...
}

WCF 中有两种会话:安全会话可靠会话wsHttpBindingnetTcpBinding 的默认设置是使用安全会话。
对于 wsHttpBinding,这是通过使用 消息级安全 来完成的客户端的凭据,这是绑定的默认设置
对于 netTcpBinding,会话是通过使用 TCP 协议的设施在传输级别建立的.
这意味着只需切换到 wsHttpBinding 或 netTcpBinding 即可启用对 WCF 会话的支持。
另一种方法是使用可靠会话。这必须在绑定配置中显式启用,并消除对 wsHttpBinding 使用消息安全性的要求。所以这会起作用:

<bindings> 
    <wshttpbinding> 
        <binding name="ReliableSessionEnabled"> 
            <reliablesession enabled="True" ordered="False" /> 
            <security mode="None" /> 
        </binding> 
    </wshttpbinding> 
</bindings>

8) False,可靠会话的使用独立于通信通道的安全设置。

更详细的解释请看this article

【讨论】:

  • 感谢您的详细解答。一个问题:在 (7) 下,您说会话是通过设置 SessionMode 属性来启用的。我发现这不是真的:在没有启用可靠会话的情况下,我得到“绑定未配置为支持会话”或类似的东西。也许现在会有所不同。
  • 我更新了帖子,提供了有关 WCF 会话的更多信息。您是否使用了其他绑定,或者您明确禁用了安全性?
  • 这是一篇有用的文章:lybecker.com/blog/2007/04/30/…
【解决方案2】:

在 Enrico 的出色回答之后,这些是我正在使用的配置:

服务:

<services>
    <service name="Foo.Bar.Service">
        <endpoint name="EndpointHttps"
            address=""
            binding="customBinding" bindingConfiguration="EndpointHttps"
            contract="Foo.Bar.IService" />
    </service>
</services>
<bindings>
    <customBinding>
        <binding name="EndpointHttps">
            <reliableSession />
            <mtomMessageEncoding />
            <httpsTransport />
        </binding>
    </customBinding>
</bindings>

客户:

<client>
    <endpoint name="EndpointHttps"
        address="https://server/FooBar/service.svc"
        binding="customBinding" bindingConfiguration="EndpointHttps"
        contract="Foo.Bar.IService" />
</client>
<bindings>
    <customBinding>
        <binding name="EndpointHttps">
            <reliableSession />
            <mtomMessageEncoding />
            <httpsTransport />
        </binding>
    </customBinding>
</bindings>

注意:尽管如此,这仍然无法与 Windows 身份验证一起使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多