【问题标题】:How to connect to WCF service via proxy如何通过代理连接到 WCF 服务
【发布时间】:2019-07-02 05:37:27
【问题描述】:

我正在尝试通过代理连接到 WCF 服务。代理通过登录密码进行身份验证。

我的代码是:

var proxyHost = "http://127.0.0.1:3128/";
var proxyUserName = "username";
var proxyUserPassword = "userpassword";

var apiConnectAddress = "https://remoteaddress/service.svc";

var binding = new BasicHttpBinding
{
    UseDefaultWebProxy = false,
    ProxyAddress = new Uri(proxyHost)
    Security =
    {
        Mode = BasicHttpSecurityMode.Transport,
        Transport = 
        {
            ClientCredentialType = HttpClientCredentialType.None,
            ProxyCredentialType = HttpProxyCredentialType.Basic
        }
    }
};

var client = new Client(binding, new EndpointAddress(apiConnectAddress));

client.ClientCredentials.UserName.UserName = proxyUserName;
client.ClientCredentials.UserName.Password = proxyUserPassword;

client.SomeMethod(...); // an exception occurs here

class Client : System.ServiceModel.ClientBase<T> { ... }

异常详情:

System.ServiceModel.ProtocolException
    HResult=0x80131500
    Message=The remote server returned an unexpected response: (407) Proxy 
    Authentication Required.
    Source=System.Private.ServiceModel
    StackTrace:
        at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
        at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
        at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
        at System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.<>c__DisplayClass1_0.<CreateGenericTask>b__0(IAsyncResult asyncResult)
        at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
        at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
        at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()

据我了解,发生异常是因为登录名和密码未传输到代理服务器。如何配置HttpBinding通过代理成功连接服务?

PS。在 .Net Core 2.2 上编写的应用

【问题讨论】:

    标签: c# wcf proxy


    【解决方案1】:

    根据提问者的提示,以及Github issue中提到的,
    github.com/dotnet/wcf/issues/1592
    目前无法使用带身份验证的代理。
    谢谢。

    【讨论】:

    • 我明白,我不需要提供客户端凭据,但我需要提供代理服务器凭据。此外,我已经生成了无需使用代理服务器即可正常工作的 WCF 客户端。在您的示例中,我没有看到任何使用代理服务器。您使用登录名和密码直接连接到服务。但我需要通过经过身份验证的代理服务器连接到服务。
    • 对不起,可能我对问题的理解有些偏差。请参考以下链接。 stackoverflow.com/questions/17968307/…绑定配置使用Proxy地址和UseDefaultWebProxy、ProxyCredentialType属性来设置凭证,希望对你有用。
    • 看来useDefaultCredentials="true" 是问题的关键。
    • 我看到了这个问题。我的代码完全一样。 useDefaultCredentials 不适合我。
    猜你喜欢
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 2023-03-29
    • 2014-06-27
    • 2011-03-14
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    相关资源
    最近更新 更多