4

输入时发现抱歉的答案

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

  • 我已经使用 Web 引用连接到同一个 Web 服务。我们只需要设置一个更大的超时时间,因为它需要很长时间才能完成。
  • 我已经连接到另一个不需要用户名/密码身份验证的 Web 服务,并使用生成的服务引用和一些设置来通过代理获取它。

所以我的想法是参考这个参考,将它指向正确的网络服务并添加身份验证。

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

    <?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>

我在身份验证调用中添加了以下代码:

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();
}

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

HTTP 请求未使用客户端身份验证方案匿名授权。接收到来自服务器的身份验证标头是 NTLM。

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

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

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

我收到以下错误:

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

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

4

2 回答 2

4

将绑定更改为:

<?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>

所以设置 authenticationScheme="Ntlm"

于 2008-11-14T09:28:54.793 回答
1

以下是无需代理即可连接的方法:

http://blog.bodurov.com/Create-a-WCF-client-for-asmx-web-service-without-using-web-proxy

于 2008-12-23T18:39:23.883 回答