从wcf客户端调用需要基本http身份验证的Web服务

Vla*_*iev 15 .net wcf web-services

我有一个来自Web服务的wsdl,我生成了wcf代理.没问题.

但我无法理解如何传递用户名和密码.Web服务需要基本身份验证 - 只有用户名和密码.

有帮助吗?

Lad*_*nka 25

是否在配置文件中配置了基本认证?您是否只需要传递凭据,还是需要安全传输(HTTPS)?

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

HTTP绑定的设置:

<bindings>
  <basicHttpBinding>
    <binding name="BasicAuth">
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Basic" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)

HTTPS绑定的设置:

<bindings>
  <basicHttpBinding>
    <binding name="BasicAuthSecured">
      <security mode="Transport">
        <transport clientCredentialType="Basic" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)

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

<client>
  <endpoint address="..." 
            name="..." 
            binding="basicHttpBinding" 
            bindingConfiguration="BasicAuth" 
            contract="..."  />
</client>
Run Code Online (Sandbox Code Playgroud)

然后,您必须将凭据传递给代理:

proxy = new MyServiceClient();
proxy.ClientCredentials.UserName.UserName = "...";
proxy.ClientCredentials.UserName.Password = "...";
Run Code Online (Sandbox Code Playgroud)


Ger*_*ski 7

对于 (A) 在 .NET Core 项目上下文中找到此答案的人以及 (B) 对代码更改而不是 XML 文件中的更改感兴趣的人:

  1. 使用dotnet-svcutil通过 WSDL 构建代码。
  2. 更新Reference.cs方法中的GetBindingForEndpoint以在 WCF 客户端中启用基本身份验证
  3. 使用客户端实例时设置登录名和密码。

示例代码:

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
    if ((endpointConfiguration == EndpointConfiguration.YourService))
    {
        System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
        result.MaxBufferSize = int.MaxValue;
        result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
        result.MaxReceivedMessageSize = int.MaxValue;
        result.AllowCookies = true;

        // Set Basic Authentication with HTTP protocol (for HTTPS you need "Transport"):
        result.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
        result.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

        return result;
    }
    throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
}
Run Code Online (Sandbox Code Playgroud)
var client = new YourServiceClient();
client.ClientCredentials.UserName.UserName = "yourservicelogin";
client.ClientCredentials.UserName.Password = "yourservicepassword";
Run Code Online (Sandbox Code Playgroud)


小智 6

参考.NET Core项目中@Gerard Jaryczewski的回答,您还可以使用以下扩展方法,因为编辑Reference.cs可能是一个挑战,因为每次更新Reference.cs后,更改都会被覆盖。

public static class BasicAuthenticationExtension
{
    public static void SetBasicAuthentication<T>(this ClientBase<T> client, string userName, string password) where T : class
    {
        if (client == null) throw new ArgumentNullException(nameof(client));
        if (client.Endpoint == null || client.Endpoint.Binding == null) throw new Exception("The specified client has no binding defined!");

        if (client.Endpoint.Binding is BasicHttpsBinding httpsBinding)
        {
            httpsBinding.Security.Mode = BasicHttpsSecurityMode.Transport;
            httpsBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
        }
        else if (client.Endpoint.Binding is BasicHttpBinding httpBinding)
        {
            httpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
            httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
        }
        else
        {
            throw new NotSupportedException("The specified client has a binding defined which is not supporting HTTP basic authentication!");
        }

        client.ClientCredentials.UserName.UserName = userName;
        client.ClientCredentials.UserName.Password = password;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你就可以像这样使用它:

var client = new MyServiceClient();
client.SetBasicAuthentication("myUserName", "myPassword");
Run Code Online (Sandbox Code Playgroud)


Pel*_*dao -1

这应该涵盖它:http ://msdn.microsoft.com/en-us/library/ms733775.aspx (请参阅客户端部分)