簡體   English   中英

使用HTTPS的WCF會話

[英]WCF sessions with HTTPS

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

我錯過了什么?

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>

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) 確實 ,必須在wsHttpBinding上啟用傳輸級安全性才能使用HTTPS

7) 虛假可靠的會話是WCF會話的可靠消息傳遞的特定實現。 可靠消息傳遞是一種WS- *標准規范,旨在保證在不可靠的網絡上傳遞消息。 您可以在沒有Reliable Messaging的情況下使用WCF會話,反之亦然。 使用此屬性在服務合同上啟用會話:

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

還要記住,為了維護服務調用之間的狀態,您將明確地必須在服務契約實現上啟用適當的實例模式:

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

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>

8) 虛假 ,可靠的會話獨立於通信信道的安全設置使用。

有關更詳細的說明,請查看本文

繼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身份驗證。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM