如何配置WCF以接受SSL和非SSL

时间:2010-08-26 19:42:03

标签: wcf configuration ssl

我需要一些帮助来配置WCF以支持多种环境。一种环境允许通过标准HTTP进行匿名身份验证,另一种环境使用基于SSL的Windows身份验证

我可以将WCF配置为支持任一环境,但不能同时支持同一web.config文件。

这是允许匿名通过http:

的内容
<behaviors>
    <serviceBehaviors>
        <behavior name="MexBehavior" >
            <serviceMetadata httpGetEnabled="true" />
        </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
        <behavior name="DLAspNetAjaxBehavior">
            <enableWebScript/>
        </behavior>
        <behavior name="Service1AspNetAjaxBehavior">
            <webHttp/>
        </behavior>
    </endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
    <service name="DL" behaviorConfiguration="MexBehavior">
        <endpoint address="" behaviorConfiguration="DLAspNetAjaxBehavior" binding="webHttpBinding" contract="DLService"/>
        <endpoint name="MEXEndpoint" contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
    </service>
    <service name="Service1" behaviorConfiguration="MexBehavior">
        <endpoint address="" behaviorConfiguration="Service1AspNetAjaxBehavior" binding="webHttpBinding" contract="Service1"/>
        <endpoint name="MEXEndpoint" contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
    </service>          
</services>

这适用于基于SSL的Windows身份验证:

<behaviors>
    <serviceBehaviors>
        <behavior name="MexBehavior" >
            <serviceMetadata httpGetEnabled="true" />
        </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
        <behavior name="DLAspNetAjaxBehavior">
            <enableWebScript/>
        </behavior>
        <behavior name="Service1AspNetAjaxBehavior">
            <webHttp/>
        </behavior>
    </endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
    <service name="DL" behaviorConfiguration="MexBehavior">
        <endpoint address="" behaviorConfiguration="DynamicLoaderAspNetAjaxBehavior" binding="webHttpBinding" bindingConfiguration="webWinBinding" contract="DLService"/>
        <endpoint name="MEXEndpoint" contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
    </service>
    <service name="Service1" behaviorConfiguration="MexBehavior">
        <endpoint address="" behaviorConfiguration="ValidValuesServiceAspNetAjaxBehavior" binding="webHttpBinding" bindingConfiguration="webWinBinding" contract="Service1"/>
        <endpoint name="MEXEndpoint" contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
    </service>          
</services>     
<bindings>
    <webHttpBinding>
        <binding name="webWinBinding">
            <security mode="Transport">
                <transport clientCredentialType="Windows" />
            </security>
        </binding>
    </webHttpBinding>           
</bindings>

当我将SSL配置中的端点添加到非SSL配置时,匿名服务会中断。

这是配置文件不起作用,但尝试将两个设置放在一起:

<behaviors>
    <serviceBehaviors>
        <behavior name="MexBehavior" >
            <serviceMetadata httpGetEnabled="true" />
        </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
        <behavior name="DLAspNetAjaxBehavior">
            <enableWebScript/>
        </behavior>
        <behavior name="Service1AspNetAjaxBehavior">
            <webHttp/>
        </behavior>
    </endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
    <service name="DynamicLoader" behaviorConfiguration="MexBehavior">
        <endpoint name="basic" address="" behaviorConfiguration="DLAspNetAjaxBehavior" binding="webHttpBinding" bindingConfiguration="webAnonymousBinding" contract="DLService"/>
        <endpoint name="secure" address="" behaviorConfiguration="DLAspNetAjaxBehavior" binding="webHttpBinding" bindingConfiguration="webWinBinding" contract="DLService"/>
        <endpoint name="MEXEndpoint" contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
    </service>
    <service name="ValidValuesService" behaviorConfiguration="MexBehavior">
        <endpoint name="basic" address="" behaviorConfiguration="Service1AspNetAjaxBehavior" binding="webHttpBinding" bindingConfiguration="webAnonymousBinding" contract="Service1"/>
        <endpoint name="secure" address="" behaviorConfiguration="Service1AspNetAjaxBehavior" binding="webHttpBinding" bindingConfiguration="webWinBinding" contract="Service1"/>
        <endpoint name="MEXEndpoint" contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
    </service>
</services>
<bindings>
    <webHttpBinding>
        <binding name="webWinBinding">
            <security mode="Transport">
                <transport clientCredentialType="Windows" />
            </security>
        </binding>
        <binding name="webAnonymousBinding">
            <security mode="None">
            </security>
        </binding>
    </webHttpBinding>
</bindings>

我是否可以通过某种方式将端点合并到一个web.config中以支持这两种环境?

1 个答案:

答案 0 :(得分:0)

这个以前的SO问题/答案是否有帮助?

calling a web service using WCF over Http and Https

(见第一个答案。)

相关问题