代码之家  ›  专栏  ›  技术社区  ›  user8032

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

  •  0
  • user8032  · 技术社区  · 16 年前

    我有一个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 回复  |  直到 16 年前
        1
  •  2
  •   Patrick McElreavy    16 年前

    据我所见,您正在实例化 HttpsTransportBindingElement 对象。您可能想使用 HttpTransportBindingElement 代替对象。

    这个 httpstransportBindingElement 对象隐式要求SSL安全性,这就是为什么 ArgumentException 那说 "The provided URI scheme 'http' is invalid; expected 'https'."

        2
  •  1
  •   CriGoT    16 年前

    你使用 HttpsTransportBindingElement 你应该使用 HttpTransportBindingElement 相反

    推荐文章