如何通过代码禁用WCF自定义绑定的安全性?

时间:2010-07-13 14:25:50

标签: wcf wcf-binding

我有一个需要调用的WCF服务。当我在VS中进行“添加服务引用”时,我得到以下客户端配置:

<configuration>
    <system.serviceModel>
        <bindings>
            <customBinding>
                <binding name="CustomBinding_IMyService">
                    <binaryMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
                        maxSessionSize="2048">
                        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    </binaryMessageEncoding>
                    <httpTransport manualAddressing="false" maxBufferPoolSize="524288"
                        maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous"
                        bypassProxyOnLocal="false" decompressionEnabled="true" 
                        hostNameComparisonMode="StrongWildcard"
                        keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous"
                        realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
                        useDefaultWebProxy="true" />
                </binding>
            </customBinding>
        </bindings>
        <client>
            <endpoint address="http://myserver/Service/MyService.svc"
                binding="customBinding" bindingConfiguration="CustomBinding_IMyService"
                contract="MyService.IMyService" name="CustomBinding_IMyService" />
        </client>
    </system.serviceModel>
</configuration>

当我使用此配置调用服务时,它可以工作,所以服务很好。但在我的实际应用程序中,我无法使用app.config,需要在代码中构建服务客户端。我试着做一个简单的翻译:

var binaryMessageEncoding = new BinaryMessageEncodingBindingElement();
var httpTransport = new HttpsTransportBindingElement()
                        {
                            Realm = "", ManualAddressing = false, 
                            HostNameComparisonMode = HostNameComparisonMode.StrongWildcard, 
                            TransferMode = TransferMode.Buffered, 
                            MaxBufferSize = int.MaxValue, 
                            MaxReceivedMessageSize = int.MaxValue, AllowCookies = false, 
                            AuthenticationScheme = AuthenticationSchemes.Anonymous, 
                            UnsafeConnectionNtlmAuthentication = false
                        };
var customBinding = new CustomBinding(binaryMessageEncoding, httpTransport);
var myServiceClient = new MyServiceClient(customBinding,
                                                    new EndpointAddress(
                                                        "http://myserver/Service/MyService.svc"));

但是当我调用该服务时,我得ArgumentException表示"The provided URI scheme 'http' is invalid; expected 'https'.",所以我出于某种原因建立的绑定认为它必须是安全的...所以,现在我们又回到了原始问题 - 如何禁用代码中CustomBinding的安全性?我知道在配置中它是一个简单的<security mode="none"/>,但我不能使用配置,并且由于某种原因无法找到等效的API。

2 个答案:

答案 0 :(得分:2)

从我看到的,你正在实例化一个HttpsTransportBindingElement对象。您可能希望使用HttpTransportBindingElement对象。

HttpsTransportBindingElement对象隐含地要求SSL安全性,这就是为什么您收到ArgumentException "The provided URI scheme 'http' is invalid; expected 'https'."

的原因

答案 1 :(得分:1)

您正在使用HttpsTransportBindingElement,而应使用HttpTransportBindingElement代替