如何配置使用 Connected Services 创建的 WSDL 客户端以使用基本身份验证?

Posted

技术标签:

【中文标题】如何配置使用 Connected Services 创建的 WSDL 客户端以使用基本身份验证?【英文标题】:How can I configure a WSDL client created with Connected Services to use Basic Authentication? 【发布时间】:2019-05-06 00:22:22 【问题描述】:

我有一个第 3 方 WSDL 服务,我需要将它集成到 .NET Core 2.1 webapp 中。

我通过“添加连接的服务”>“Microsoft WCF Web 服务参考提供程序”添加了该服务。

我有一些像这样配置服务的实用程序类:

ServiceClient servicePort = new ServiceClient(); 

servicePort.ClientCredentials.UserName.UserName = USERNAME;
servicePort.ClientCredentials.UserName.Password = PASSWORD;

setupHttpHeader(servicePort.InnerChannel );

在哪里

    public static void setupHttpHeader( IClientChannel serviceChannel )
    
        using (new OperationContextScope( serviceChannel ))
        
            // Add a HTTP Header to an outgoing request
            HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
            requestMessage.Headers["Authorization"] = "Basic " + System.Convert.ToBase64String(Encoding.UTF8.GetBytes(USERNAME + ":" + PASSWORD));
            OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
        
    

现在,当我尝试在我的控制器中调用端点时,我得到了这个异常:

System.ServiceModel.Security.MessageSecurityException: The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'basic realm="appbox object-model"'.     

在用于连接第 3 方服务的旧 .NET Framework 4.0 示例中,有一个 app.config 具有一些绑定配置。他们看起来像这样:

      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Basic" />
      </security> 

我需要在我的项目中添加什么才能完成这项工作?

【问题讨论】:

您确认标头是否确实包含 auth 标头? 标头不包含 auth 标头。添加标题的代码已从旧示例中复制并粘贴。明天我会再试一次,看看如何正确添加标题。 也许可以尝试删除 transportonly 安全和安全模式,将它们设置为 none 并让您的应用程序执行 auth header 来完成其余的工作 【参考方案1】:

解决方案在this issue comment找到。

在生成代码的GetBindingForEndpoint 部分中,需要有类似的内容:

System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding(System.ServiceModel.BasicHttpSecurityMode.Transport);
result.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Basic;

在我的情况下,我需要TransportCredentialOnly,因为端点是http 而不是https

【讨论】:

以上是关于如何配置使用 Connected Services 创建的 WSDL 客户端以使用基本身份验证?的主要内容,如果未能解决你的问题,请参考以下文章

如何配置报表服务器扩展部署(Reporting Services 配置)

在游戏框架中,secure.connected() 是如何工作的?

如何使用 python 编码读取特定网站的控制台日志(例如,ok、connected 等消息)?

我们如何在 services.AddAzureClients 上配置 HttpProxy

k8s基本概念-如何使用Services

如何使用 redux-thunk 调度操作让 connected-react-router 重定向?