【问题标题】:ASP.NET Core 3.1/5.0 Core Consume SOAP - Basic Authentication problemASP.NET Core 3.1/5.0 Core 使用 SOAP - 基本身份验证问题
【发布时间】:2021-11-10 14:08:03
【问题描述】:

我需要帮助在 ASP.NET 5.0 Core/ASP.NET 3.1 Core (VS2019) 中配置带有基本身份验证 (http) 的 SOAP 请求。

我已经构建了一个 ASP.NET Core Web API (.NET 3.1/5.0) 项目,以便使用具有基本身份验证的 SOAP 服务。但是我的代码无法将基本身份验证数据发送到服务,当我在 VS2019 中运行项目时,我得到了这个异常:

抛出异常:System.Private.CoreLib.dll 中的“System.ServiceModel.Security.MessageSecurityException” System.Private.CoreLib.dll 中出现“System.ServiceModel.Security.MessageSecurityException”类型的异常,但未在用户代码中处理 HTTP 请求未经客户端身份验证方案“匿名”授权。从服务器收到的身份验证标头是 'Basic realm="test"'

SOAP 服务由 .NET Core“连接服务”(Microsoft WCF Web 服务参考提供者)和 WSDL 配置。

我正在尝试使用以下代码创建 http 绑定:

MyService.UpdateReq invoiceInfo = new MyService.UpdateReq();
MyService.JRPClient updateREQ = new MyService.JRPClient();

((BasicHttpBinding)updateREQ.Endpoint.Binding).Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

((BasicHttpBinding)updateREQ.Endpoint.Binding).Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic;


updateREQ.ClientCredentials.UserName.UserName = "username";
updateREQ.ClientCredentials.UserName.Password = "pwd";

MyService.ResponseType response = new MyService.ResponseType();

response = updateREQ.UpdateReq(invoiceInfo);

我还更改了 %PortTypeChannel 文件:

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
        {
            
        
            System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
            result.Security.Mode = BasicHttpSecurityMode.Transport; 
            result.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
            result.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic;

return result;

但现在它失败并显示其他消息:

抛出异常:System.Private.ServiceModel.dll 中的“System.ArgumentException” System.Private.ServiceModel.dll 中出现“System.ArgumentException”类型的异常,但未在用户代码中处理 提供的 URI 方案“http”无效;预期为“https”

我用soapUI尝试了soap服务,添加了基本身份验证,它就可以工作了。

谢谢

【问题讨论】:

    标签: asp.net-core wcf asp.net-web-api web-applications


    【解决方案1】:

    对我有用的解决方案是将用户和密码编码为 Base64:

    MyService.JRPClient updateREQ = new MyService.JRPClient(); // SOAP Client 
    
    updateREQ.ClientCredentials.UserName.UserName = "username";
    updateREQ.ClientCredentials.UserName.Password = "pwd";
    
    MyService.ResponseType response = new MyService.ResponseType();
    
    using (OperationContextScope scope = new OperationContextScope(updateREQ.InnerChannel)) 
    {
        var httpRequestProperty = new HttpRequestMessageProperty();
        httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(updateREQ.ClientCredentials.UserName.UserName + ":" + updateREQ.ClientCredentials.UserName.Password));
    
        OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
    
        response = updateREQ.UpdateReq(invoiceInfo); // Consume SOAP service
    }
    

    【讨论】:

      猜你喜欢
      • 2021-02-11
      • 2021-04-16
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 2020-07-27
      • 1970-01-01
      相关资源
      最近更新 更多