强制WCF使用HTTPS

问题描述:

如何确保我的WCF服务已通过HTTPS并且所有通信均通过HTTPS?强制WCF使用HTTPS

在服务我的web.config文件

即使世界没什么可说的http:// ...或https:// ....

HTTPS是在IIS中设置,我可以访问Web服务通过http和https。

或者,如果使用消息级别的安全性进行加密,这不是必需的吗?

<bindings> 
    <wsHttpBinding> 
    <binding name="Binding1" maxReceivedMessageSize="20000000"> 
     <security mode="Message"> 
     <message clientCredentialType="UserName"/> 
     </security> 
    </binding> 
    </wsHttpBinding> 
</bindings> 

HTTPS是基于传输层安全性(TLS)的HTTP。要启用它,你需要配置你的绑定,除了现有的消息级安全使用传输安全机制:

<bindings> 
    <wsHttpBinding> 
    <binding name="Binding1" maxReceivedMessageSize="20000000"> 
     <security mode="TransportWithMessageCredential"> 
     <message clientCredentialType="UserName"/> 
     </security> 
    </binding> 
    </wsHttpBinding> 
</bindings> 
+0

谢谢,根据@Vano Maisuradze的回答,是否必须将''更改为''?这似乎工作。 – Marcus 2011-12-29 14:53:38

+0

这只会改变您的服务本身(网络服务位),而不会改变元数据交换或用户浏览服务发现页面的功能(当您在浏览器中转到http://domain/myservice.svc时)。如果这些必须通过HTTPS工作,您需要进一步,如Vano所述。 – 2011-12-29 15:04:34

这里是HTTPS配置:

<services> 
    <service name="WcfTransport.Service1" behaviorConfiguration="MyHttpsBehaviour"> 
    <endpoint address="" binding="wsHttpBinding" bindingConfiguration="TransportSecurityBinding" contract="WcfTransport.IService1"/> 
    <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" /> 
    </service> 
</services> 

<behaviors> 
    <serviceBehaviors > 
    <behavior name="MyHttpsBehaviour" > 
     <serviceMetadata httpsGetEnabled="true" httpGetEnabled="false" /> 
     <serviceDebug includeExceptionDetailInFaults="true"/> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 

<bindings> 
    <wsHttpBinding> 
    <binding name="TransportSecurityBinding"> 
     <security mode="Transport"> 
     <transport clientCredentialType="Windows"></transport> 
     </security> 
    </binding> 
    </wsHttpBinding> 
</bindings> 

注意httpsGetEnabled设置为true和httpGetEnabled设置为false。如果您不需要元数据交换,也可以删除mex端点。

p.s.消息安全性用于消息加密,但当然,您可以使用https使用消息安全性。

+0

所以只是做''品牌它通过HTTPS?它似乎工作,但我现在不确定基于@Programming Hero的回答 – Marcus 2011-12-29 14:52:56

+0

是的。它只接受https请求。您也可以在IIS中删除http绑定并仅添加https绑定。 – 2011-12-29 15:26:47