简体   繁体   English

使用HTTPS的WCF会话

[英]WCF sessions with HTTPS

I cannot figure out how to enable per-session instances for my WCF service while using HTTPS. 我无法弄清楚如何在使用HTTPS时为我的WCF服务启用每会话实例。 (I'm not an ASP.NET expert but don't want to use ASP.NET session state if possible.) I am using .NET Framework 3.0. (我不是ASP.NET专家,但如果可能的话,不想使用ASP.NET会话状态。)我使用的是.NET Framework 3.0。

I have arrived at the following contradiction and am hoping that someone can tell me where there is a flaw in the logic. 我已经达到了以下矛盾,我希望有人可以告诉我逻辑中存在缺陷的地方。

1) The service must be hosted on IIS 6 due to client mandate. 1)由于客户的要求,该服务必须托管在IIS 6上。

2) The service needs to maintain state between calls, including SqlConnection and SqlTransaction instances (ugly but necessary due to project constraints). 2)服务需要在调用之间维护状态,包括SqlConnection和SqlTransaction实例(由于项目限制,丑陋但必要)。

3) Therefore I need to use the wsHttpBinding. 3)因此我需要使用wsHttpBinding。

4) The service needs to be able to access user authentication info from HttpContext.Current.User.Identity (eg using Windows security in IIS). 4)服务需要能够从HttpContext.Current.User.Identity访问用户身份验证信息(例如,在IIS中使用Windows安全性)。

5) HTTPS is therefore required. 5)因此需要HTTPS。

6) Transport-level security must therefore be configured on the binding. 6)因此必须在绑定上配置传输级安全性。

7) Configuring the service to require sessions means I have to configure the wsHttpBinding to use Reliable Sessions. 7)配置服务以要求会话意味着我必须配置wsHttpBinding以使用可靠会话。

8) This requires that message-level security is configured on the binding. 8)这要求在绑定上配置消息级安全性。

Ie (6) and (8) are mutually exclusive. 即(6)和(8)是相互排斥的。

It seems that using WCF sessions requires that I use message-level security, which prevents me from using HTTPS. 似乎使用WCF会话要求我使用消息级安全性,这会阻止我使用HTTPS。

What am I missing? 我错过了什么?

3) True , wsHttpBinding and wsDualHttpBinding are the only HTTP bindings that support sessions 3) 是的wsHttpBindingwsDualHttpBinding是唯一支持会话的HTTP绑定

5) False , in order to authenticate the service callers you don't necessarily need to have any transport-level security (such as SSL/HTTPS). 5) 错误 ,为了验证服务调用者,您不一定需要具有任何传输级安全性(例如SSL / HTTPS)。 The only requirement is to configure IIS to enable Integrated Windows Authentication for a virtual directory. 唯一的要求是配置IIS以启用虚拟目录的集成Windows身份验证 Then in WCF you have three possibilities to enable this scenario: 然后在WCF中,您有三种可能性来启用此方案:

a) Use transport-level security on the wsHttpBinding with Windows credentials (HTTPS) a)使用Windows凭据(HTTPS)在wsHttpBinding上使用传输级安全性

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

b) Use message-level security on the wsHttpBinding with Windows credentials (HTTP) b)使用Windows凭据(HTTP)在wsHttpBinding上使用消息级安全性

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

c) Run your service under the ASP.NET Compatibility Mode and enable Windows Authentication in ASP.NET (HTTP) c)在ASP.NET兼容模式下运行您的服务并在ASP.NET(HTTP)中启用Windows身份验证

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

Note that in a and b you will access the identity of the caller from within a service this way: 请注意,在ab中,您将以这种方式从服务中访问调用者的身份:

OperationContext.Current.ServiceSecurityContext.WindowsIdentity

6) True , transport-level security must be enabled on the wsHttpBinding in order to use HTTPS 6) 确实 ,必须在wsHttpBinding上启用传输级安全性才能使用HTTPS

7) False , Reliable Sessions is a particular implementation of Reliable Messaging for WCF sessions. 7) 虚假可靠的会话是WCF会话的可靠消息传递的特定实现。 Reliable Messaging is a WS-* standard specification designed to guarantee message delivery on an unreliable network. 可靠消息传递是一种WS- *标准规范,旨在保证在不可靠的网络上传递消息。 You can use WCF sessions without Reliable Messaging, and viceversa. 您可以在没有Reliable Messaging的情况下使用WCF会话,反之亦然。 Sessions are enabled on the service contract with this attribute: 使用此属性在服务合同上启用会话:

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

Also remember that in order to maintain state between service calls you will explicitly have to enable the appropriate instance mode on the service contract implementation: 还要记住,为了维护服务调用之间的状态,您将明确地必须在服务契约实现上启用适当的实例模式:

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

There are two kinds of sessions in WCF: Secure Sessions and Reliable Sessions . WCF中有两种会话: 安全会话可靠会话 The default setting for both wsHttpBinding and netTcpBinding is to use Secure Sessions. wsHttpBindingnetTcpBinding的默认设置是使用安全会话。
For wsHttpBinding this is accomplished with message-level security by using the client's credentials, which is the default setting for the binding. 对于wsHttpBinding,这是通过使用客户端凭据实现的消息级安全性 ,这是绑定的默认设置
For netTcpBinding instead, the session is established at the tranport level by using the facilities of the TCP protocol. 相反,对于netTcpBinding,通过使用TCP协议的功能在传输级别建立会话。
This means that simply switching to wsHttpBinding or netTcpBinding will enable support for WCF sessions. 这意味着只需切换到wsHttpBinding或netTcpBinding即可启用对WCF会话的支持。
The alternative is to use Reliable Sessions . 另一种方法是使用Reliable Sessions This has to explicitly be enabled in the binding configuration, and removes the requirement of using message security for the wsHttpBinding. 这必须在绑定配置中明确启用,并消除了对wsHttpBinding使用消息安全性的要求。 So this will work: 所以这将有效:

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

8) False , Reliable Sessions are used independently of the security settings of the communication channel. 8) 虚假 ,可靠的会话独立于通信信道的安全设置使用。

For a more detailed explanation, have a look at this article . 有关更详细的说明,请查看本文

Following through on Enrico's excellent answer, these are the configs I am using: 继Enrico的优秀答案之后,这些是我正在使用的配置:

Service: 服务:

<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: 客户:

<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>

Note: still haven't gotten this to work with Windows authentication though. 注意:尽管如此,仍然没有使用Windows身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM