WCF会话与HTTPS

8

我无法弄清如何在使用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。

我错过了什么?


你是遇到了特定的配置错误,还是在检查这个配置是否可行? - Agent_9191
我想知道是否可行:即使用HTTPS进行WCF会话配置。谢谢。 - Ian Horwill
2个回答

16

3) 只有 True, wsHttpBinding, 和 wsDualHttpBinding 这三种 HTTP 绑定支持会话。

5) False,为了认证服务调用者,您不一定需要任何传输级别的安全性(如 SSL/HTTPS)。唯一的要求是在虚拟目录中配置 IIS 以启用 Integrated Windows Authentication。然后在 WCF 中,您有三种可能的选择来启用此方案:

a) 使用传输级别安全性,在 wsHttpBinding 上使用 Windows 凭据(HTTPS)

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

b) 在使用基于 HTTP 的 wsHttpBinding 时,使用基于消息的安全性和 Windows 凭据。

<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身份验证,同时开启ASP.NET兼容性模式

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

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

OperationContext.Current.ServiceSecurityContext.WindowsIdentity

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

7) False, 可靠会话(Reliable Sessions) 是 WCF 会话的一种特定实现,而可靠消息(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, 可靠会话(Reliable Sessions)与通信渠道的安全机制是独立使用的。

想要更详细的解释,请查看这篇文章


感谢您详细的回答。我有一个问题:在(7)下,您说会话是通过设置SessionMode属性来启用的。但我发现这并不正确:如果没有启用可靠的会话,我会得到“绑定未配置为支持会话”之类的错误。也许现在情况已经改变了。 - Ian Horwill
我更新了关于WCF会话的帖子,并添加了一些更多的信息。你可能在使用另一个绑定,或者你明确禁用了安全性吗? - Enrico Campidoglio
这是一篇有用的文章:http://www.lybecker.com/blog/2007/04/30/wcf-sessions-and-reliable-messaging/ - Enrico Campidoglio

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身份验证使其工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接