C# 具有登录密码身份验证的WCF客户端

C# 具有登录密码身份验证的WCF客户端,c#,web-services,wcf,C#,Web Services,Wcf,我正在尝试连接到非现场asmx服务。我有用于身份验证的asmx url和登录密码。我已将服务引用添加到该服务,并使用配置VS 2010生成WCF客户端: <system.serviceModel> <bindings> <basicHttpBinding> <binding name="serviceSoap" /> </basicHttpBinding>

我正在尝试连接到非现场asmx服务。我有用于身份验证的asmx url和登录密码。我已将服务引用添加到该服务,并使用配置VS 2010生成WCF客户端:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="serviceSoap" />
        </basicHttpBinding>
        <customBinding>
            <binding name="serviceSoap12">
                <textMessageEncoding messageVersion="Soap12" />
                <httpTransport />
            </binding>
        </customBinding>
    </bindings>
    <client>
        <endpoint address="http://tourml.danko.ru:9191/Service.asmx"
            binding="basicHttpBinding" bindingConfiguration="serviceSoap"
            contract="DankoTourMLService.serviceSoap" name="serviceSoap" />
        <endpoint address="http://tourml.danko.ru:9191/Service.asmx"
            binding="customBinding" bindingConfiguration="serviceSoap12"
            contract="DankoTourMLService.serviceSoap" name="serviceSoap12" />
    </client>
但这里抛出System.ServiceModel.Security.MessageSecurityException:

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was ''.
对于内部System.Net.WebException:

{"The remote server returned an error: (401) Unauthorized."}
ClientCredentials设置有什么问题?我应该使用另一个绑定,而不是basicHttpBinding吗?

根据上述情况。当需要指定凭据时,端点应设置安全性。在您的配置中并非如此

两种可能性=>addservice引用无法创建正确的配置(不太可能)。 第二个=>您必须在服务上而不是在传输上指定凭据

var service = new serviceSoapClient("serviceSoap");

//service.ClientCredentials.UserName.UserName = username;
//service.ClientCredentials.UserName.Password = password;            

service.setCredentials() (or something)
var itmes = service.getItems();

你应该查阅该服务的文档。

我知道这是一篇老文章,但最近我遇到了同样的问题,可能会帮助其他人我是如何解决的。我有一个.net web应用程序,为支付系统使用SOAP服务,具有基本身份验证。解决方案是使用用户名前面带有“u”的客户端凭据,并在web.config中指定绑定传输

client.ClientCredentials.UserName.UserName = "uMyUsername";
client.ClientCredentials.UserName.Password = "MyPassword";
在web.config中:

<bindings>
  <basicHttpBinding>
    <binding name="XYZ">
      <security mode="Transport" >
        <transport clientCredentialType="Basic"/>
      </security>
    </binding>
    <binding name="XYZ" />
  </basicHttpBinding>
</bindings>


尝试这样创建服务=>var service=new serviceSoapClient();您可能指定了错误的绑定。我尝试了这两种绑定,但出现了相同的错误
<bindings>
  <basicHttpBinding>
    <binding name="XYZ">
      <security mode="Transport" >
        <transport clientCredentialType="Basic"/>
      </security>
    </binding>
    <binding name="XYZ" />
  </basicHttpBinding>
</bindings>