C# 通过代理使用 WCF 连接到 asmx Web 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/289601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:49:49  来源:igfitidea点击:

Connecting to an asmx webservice with WCF through a proxy

c#wcfauthenticationproxyasmx

提问by KeesDijk

Sorry answer found while typing

打字时找到抱歉的答案

I am trying to connect to an external webservice that requires username/password authentication through a proxy. I am using Visual Studio Express 2008 to generate a service reference

我正在尝试通过代理连接到需要用户名/密码身份验证的外部 Web 服务。我正在使用 Visual Studio Express 2008 生成服务引用

  • I have connected to the same webservice using a web reference.We only had to set a larger timeout because it takes a long time to finish.
  • I have connected to another webservice that does not require username/password authentication with a generated service reference and some settings to get it through the proxy.
  • 我已经使用网络引用连接到同一个网络服务。我们只需要设置一个更大的超时,因为它需要很长时间才能完成。
  • 我已经连接到另一个不需要用户名/密码身份验证的 Web 服务,并使用生成的服务引用和一些设置来通过代理获取它。

So my thought would be to take this reference, point it to the correct webservice and add authentication.

所以我的想法是获取此参考,将其指向正确的 Web 服务并添加身份验证。

The config I am using without security:

我在没有安全性的情况下使用的配置:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.net>
        <defaultProxy useDefaultCredentials="true">
          <proxy bypassonlocal="False" proxyaddress="http://***.***.****:80" />
        </defaultProxy>
      </system.net>
        <system.serviceModel>
          <bindings>
            <customBinding>
              <binding name="AreaWebServiceSoap12">
                <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
                    messageVersion="Soap12" writeEncoding="utf-8">
                  <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                      maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                </textMessageEncoding>            
                <httpTransport manualAddressing="false" maxBufferPoolSize="524288"
                    maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous"
                    bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous"
                    realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
                    useDefaultWebProxy="true" />            
              </binding>
            </customBinding>
          </bindings>
          <client>
            <endpoint address="http://www.****.*****.****.com/samplewebservice/service.asmx"
                    binding="customBinding" bindingConfiguration="AreaWebServiceSoap12"
                    contract="ServiceReference1.ServiceSoap" name="ServiceSoap" />
            </client>
        </system.serviceModel>
    </configuration>

I have added te following code to my call for authentication:

我已将以下代码添加到我的身份验证调用中:

static void Main(string[] args)
{
  ServiceSoapClient s = new ServiceSoapClient();
  s.ClientCredentials.UserName.UserName = @"username";
  s.ClientCredentials.UserName.Password = @"password";

  Service.RawGpsData[] result = s.GetRawGpsData(0);
  Console.WriteLine(String.Format("done:{0}",result.Length));
  Console.ReadLine();
}

Just using this setup gives an error as expected:

仅使用此设置会按预期出现错误:

The HTTP request is not authorized with client authentication scheme Anonymous. The authentication header from the server is received, is NTLM.

HTTP 请求未通过客户端身份验证方案 Anonymous 授权。收到来自服务器的身份验证标头,是 NTLM。

Now I get lost and start trying silly things because I am just starting to use WCF.

现在我迷路了并开始尝试愚蠢的事情,因为我刚刚开始使用 WCF。

When I add the following section to the config

当我将以下部分添加到配置时

 <security authenticationMode="UserNameOverTransport"></security>

I get the following error:

我收到以下错误:

The binding CustomBinding.http: / / tempuri.org / for the contract AreaWebServiceSoap.AreaWebServices is configured with a verification mode for which a transport level with integrity and confidentiality is required. The transport can not provide integrity and confidentiality.

合约AreaWebServiceSoap.AreaWebServices 的绑定CustomBinding.http://tempuri.org/ 配置了验证模式,该模式需要具有完整性和机密性的传输级别。传输不能提供完整性和机密性。

Sorry, while typing this question I stumbled upon the answer myself. I still think people might be interested in this and all comments and thoughts are still welcome. So I will leave the question here and make it community and post the answer myself.

抱歉,在输入这​​个问题时,我自己偶然发现了答案。我仍然认为人们可能对此感兴趣,并且仍然欢迎所有评论和想法。所以我会把这个问题留在这里,让它成为社区并自己发布答案。

采纳答案by KeesDijk

Change the binding to :

将绑定更改为:

<?xml version="1.0" encoding="utf-8" ?>
<customBinding>
            <binding name="AreaWebServiceSoap12" closeTimeout="00:01:00" openTimeout="00:10:00"
                    receiveTimeout="00:20:00" sendTimeout="00:05:00">
                <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
                  messageVersion="Soap12" writeEncoding="utf-8">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
              </textMessageEncoding>              
              <httpTransport manualAddressing="false" maxBufferPoolSize="524288"  
                  maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Ntlm"
                  bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                  keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous"
                  realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
                  useDefaultWebProxy="true" />              
            </binding>
          </customBinding>

So set authenticationScheme="Ntlm"

所以设置 authenticationScheme="Ntlm"