【问题标题】:HTTP proxy basic authentication with WCF ChannelBase使用 WCF ChannelBase 的 HTTP 代理基本身份验证
【发布时间】:2011-07-04 12:11:21
【问题描述】:

我遇到了连接 Web 服务的基本身份验证问题,该服务是在 Visual Studio 中从 ChannelBase 派生的。已经尝试在MSDN中询问没有结果,所以我在这里碰碰运气。

这是场景:

a) Web 服务需要基本身份验证(用户名和密码)并支持 SOAP 1.1

b) 我必须通过 HTTP 代理,这又需要基本身份验证。

我无法让它工作:Web 服务上的身份验证很好(如果测试绕过代理)但看起来我无法配置 ChannelBase 派生类来处理代理上的身份验证(在获得所需的身份验证之后HTTP 响应,它不会继续协商身份验证)。

这是我在 app.config 中的当前设置

<configuration>
 <system.serviceModel>
  <bindings>
   <customBinding>
    <binding name="LocationWSBinding" receiveTimeout="00:02:30">
     <textMessageEncoding messageVersion="Soap11" />
     <httpTransport authenticationScheme="Basic" proxyAddress="http://The.proxy.ip" useDefaultWebProxy="false" 
      bypassProxyOnLocal="True" proxyAuthenticationScheme="Basic" />
    </binding>
   </customBinding>
  </bindings>
  <client>
   <endpoint address="http://the.web.service"
      binding ="customBinding" bindingConfiguration="LocationWSBinding"
     contract="The.Contract.Name" name="LocationWSPort" />
  </client>
 </system.serviceModel>
</configuration>

在代码中,这是我指定凭据的地方:

TheClient WS = new TheClient("LocationWSPort");

  WS.ClientCredentials.UserName.UserName = "WebServiceUsername";
  WS.ClientCredentials.UserName.Password = "WebServicepassword";
  WS.ChannelFactory.Credentials.Windows.ClientCredential = new NetworkCredential("Proxyusername","ProxyPassword");

你能帮帮我吗?

我不知道是什么问题阻止了客户端在请求时继续进行代理身份验证。

感谢伊曼纽尔


Enrico,抱歉回复晚了。 我已尝试遵循您的建议,并且仅当我调用不带参数的 web 服务方法时才有效,当我调用需要复杂类型作为参数的方法时(这是我必须进行的真正调用才能获得有用的有效负载),我得到以下信息错误:

{"The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error."}
[System.ServiceModel.CommunicationException]: {"The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error."}
_className: null
_data: {System.Collections.ListDictionaryInternal}
_dynamicMethods: null
_exceptionMethod: null
_exceptionMethodString: null
_helpURL: null
_HResult: -2146233087
_innerException: null
_ipForWatsonBuckets: 83189596
_message: "The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error."
_remoteStackIndex: 1
_remoteStackTraceString: "\r\nServer stack trace: \r\n   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)\r\n   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)\r\n   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)\r\n\r\nException rethrown at [0]: \r\n"
_safeSerializationManager: {System.Runtime.Serialization.SafeSerializationManager}
_source: null
_stackTrace: {sbyte[80]}
_stackTraceString: null
_watsonBuckets: null
_xcode: -532462766
_xptrs: 0
Data: {System.Collections.ListDictionaryInternal}
HelpLink: null
HResult: -2146233087
InnerException: null
IsTransient: false
Message: "The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error."
Source: "mscorlib"
StackTrace: "\r\nServer stack trace: \r\n   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)\r\n   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)\r\n   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)\r\n\r\nException rethrown at [0]: \r\n   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)\r\n   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)\r\n   at [.......] in E:\\VisualStudioPrj\\HTTPproxyTroubleshoot\\HTTPproxyTroubleshoot\\Program.cs:line 88"
TargetSite: {Void HandleReturnMessage(System.Runtime.Remoting.Messaging.IMessage, System.Runtime.Remoting.Messaging.IMessage)}

现在,我确信数据协定没问题(如果我没有设置 WebRequest.DefaultWebProxy,它就可以工作)。 至于 FTP,嗯,这些 SOAP 调用是由 Windows 服务运行的两个线程之一完成的将被格式化为破坏 FTP 通信的 HTML...

【问题讨论】:

    标签: wcf authentication proxy wcf-client


    【解决方案1】:

    您遇到的问题是由于WCF will use the same set of client credentials both for service authentication and for upstream web proxy authentication.由于您的服务需要与上游网络代理不同的一对凭据,因此请求未正确验证并随后被阻止。

    由于您只能在 ClientCredentials 类上指定一组凭据,因此解决方案是设置凭据以在较低级别与 Web 代理一起使用,使用 WebRequest.DefaultWebProxy属性。

    这是一个例子:

    var webProxy = new WebProxy
    {
        Address = "http://myproxy:8080",
        Credentials = new NetworkCredential("username", "password")
    };
    
    WebRequest.DefaultWebProxy = webProxy;
    

    然后您应该告诉 WCF 使用刚刚通过 WebProxy 类配置的默认代理:

    <bindings>
        <customBinding>
            <binding name="MyBinding">
                <textMessageEncoding messageVersion="Soap11" />
                <httpTransport authenticationScheme="Basic"
                               useDefaultWebProxy="true" 
                               bypassProxyOnLocal="true"
                               proxyAuthenticationScheme="Basic" />
            </binding>
        </customBinding>
    </bindings>
    

    相关资源:

    【讨论】:

    • Ciao Enrico!好吧,我想到了,但我必须使用 System.ServiceModel.Channels 命名空间中的 ChannelBase - 所以我不确定如何使用 System.Net.WebRequest 中的 WebProxy 来解决问题。 . Grazie del suggerimento...
    • 忘记添加...相同的服务需要在单独的线程上使用 System.Net 发出 FTP 请求 - 如果请求通过代理,我会收到充满 HTML 的 FTP 响应 - 我猜我可以覆盖代理设置来设置一个空代理 - 但是我不能使用 useDefaultWebProxy=true ......
    • @eddo System.Net.WebRequest 及其派生类是一个低级 API,WCF 在内部使用它。因此,将WebRequest.DefaultWebProxy 属性设置为自定义的System.Net.WebProxy 对象将自动影响所有传出消息。关于 FTP 请求,我不确定我是否在关注你。客户端会发出 FTP 请求吗?
    猜你喜欢
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 2010-09-16
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多