使用HTTPS的WCF会话

Ian*_*ill 8 c# https wcf iis-6

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

我错过了什么?

Enr*_*lio 16

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

5)错误,为了验证服务调用者,您不一定需要具有任何传输级安全性(例如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>
Run Code Online (Sandbox Code Playgroud)

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

<system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding name="SecurityEnabledWsHttp">
                <security mode="Message">
                    <message clientCredentialType="Windows" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

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

<system.web>
    <authentication mode="Windows" />
</system.web>
Run Code Online (Sandbox Code Playgroud)

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

OperationContext.Current.ServiceSecurityContext.WindowsIdentity
Run Code Online (Sandbox Code Playgroud)

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

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

[ServiceContract(SessionMode=SessionMode.Required)]
public interface IMyService {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

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

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
public class MyService : IMyService {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

WCF中有两种会话:安全会话可靠会话.wsHttpBindingnetTcpBinding的默认设置是使用安全会话.
对于wsHttpBinding,这是通过使用客户端凭据实现的消息级安全性,这是绑定的默认设置.
相反,对于netTcpBinding,通过使用TCP协议的功能在传输级别建立会话.
这意味着只需切换到wsHttpBinding或netTcpBinding即可启用对WCF会话的支持.
另一种方法是使用Reliable Sessions.这必须在绑定配置中明确启用,并消除了对wsHttpBinding使用消息安全性的要求.所以这将有效:

<bindings> 
    <wshttpbinding> 
        <binding name="ReliableSessionEnabled"> 
            <reliablesession enabled="True" ordered="False" /> 
            <security mode="None" /> 
        </binding> 
    </wshttpbinding> 
</bindings>
Run Code Online (Sandbox Code Playgroud)

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

有关更详细的说明,请查看本文.