具有登录密码身份验证的wcf客户端

本文关键字:wcf 客户端 身份验证 密码 登录 | 更新日期: 2023-09-27 17:50:05

我正在尝试连接非站点asmx-service。我有asmx url和登录密码进行身份验证。我已经添加了服务引用到服务和VS 2010生成的wcf客户端config:

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

我已经创建了客户端与basicHttpBinding和设置凭据。然后我尝试调用service方法:

var service = new serviceSoapClient("serviceSoap");
service.ClientCredentials.UserName.UserName = username;
service.ClientCredentials.UserName.Password = password;            
var items = service.GetItemList();

但是这里System.ServiceModel.Security.MessageSecurityException被抛出:

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was ''.

与内部system.net.webeexception:

{"The remote server returned an error: (401) Unauthorized."}

ClientCredentials设置有什么问题?我应该使用另一个绑定,而不是basicHttpBinding吗?

具有登录密码身份验证的wcf客户端

http://msdn.microsoft.com/en-us/library/ms732391(v=vs.110).aspx根据上述。当您需要指定凭据时,端点应该具有安全设置。在您的配置中不是这样的。

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

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>