簡體   English   中英

從 wcf 客戶端調用需要基本 http 身份驗證的 Web 服務

[英]Calling a web service that requires basic http authentication from wcf client

我有一個來自 Web 服務的 wsdl,我生成了 wcf 代理。 沒問題。

但是我不知道如何傳遞用戶名和密碼。 Web 服務需要基本身份驗證 - 僅用戶名和密碼。

有什么幫助嗎?

配置文件中是否配置了Basic認證? 您只需要傳遞憑據還是還需要安全傳輸 (HTTPS)?

首先需要設置綁定支持Basic認證

HTTP 綁定的設置:

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

HTTPS 綁定的設置:

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

客戶端端點必須使用定義的配置,例如:

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

然后您必須將憑據傳遞給代理:

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

對於 (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));
}
var client = new YourServiceClient();
client.ClientCredentials.UserName.UserName = "yourservicelogin";
client.ClientCredentials.UserName.Password = "yourservicepassword";

參考.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;
    }
}

然后你可以像這樣使用它:

var client = new MyServiceClient();
client.SetBasicAuthentication("myUserName", "myPassword");

這應該涵蓋它:http: //msdn.microsoft.com/en-us/library/ms733775.aspx (參見客戶端部分)

我會說這可能取決於 Web 服務希望您如何傳遞信息。 畢竟,你只是消費者。

話雖如此,Web 服務通常會在 SOAP 標頭中傳遞用戶 ID 和密碼。

您可以參考此鏈接以獲取此場景的示例實現

示例肥皂消息

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <AUTHHEADER xmlns="http://tempuri.org/">
      <USERNAME>string</USERNAME>
      <PASSWORD>string</PASSWORD>
    </AUTHHEADER>
  </soap:Header>
  <soap:Body>
    <SENSITIVEDATA xmlns="http://tempuri.org/" />
  </soap:Body>
</soap:Envelope>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM