调用,需要从WCF客户端基本的HTTP身份验证的Web服务身份验证、客户端、基本、WCF

2023-09-03 11:06:31 作者:老板,来碗泪流满面

我有一个Web服务的WSDL,我产生了WCF代理。没问题。

I have a wsdl from a web service, I generated the wcf proxy. No problem.

但我不能让我的头,围绕如何通过用户名和密码。该Web服务需要基本身份验证 - 只有用户名和密码

But I can not get my head around how to pass the user name and password. The webservice requires basic authentication - only username and password.

任何帮助吗?

推荐答案

被配置为配置文件中的基本身份验证?你需要传递唯一凭据或做你也需要安全的交通工具(HTTPS)?

Is Basic authentication configured in configuration file? Do you need to pass only credentials or do you also need secured transport (HTTPS)?

首先,您需要设置绑定,支持基本身份验证

First you need to set up binding to support Basic authentication

设置为HTTP绑定:

<bindings>
  <basicHttpBinding>
    <binding name="BasicAuth">
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Basic" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>

设置为HTTPS绑定:

Setup for HTTPS binding:

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

客户端的端点必须使用定义的配置,如:

Client endpoint has to use defined configuration like:

<client>
  <endpoint address="..." 
            name="..." 
            binding="basicHttpBinding" 
            bindingConfiguration="BasicAuth" 
            contract="..."  />
</client>

然后,你必须传递凭据到代理:

Then you have to pass credentials to the proxy:

proxy = new MyServiceClient();
proxy.ClientCredentials.UserName.UserName = "...";
proxy.ClientCredentials.UserName.Password = "...";