【问题标题】:Trying to connect ASP.NET CORE 2 Web API app to WCF service尝试将 ASP.NET CORE 2 Web API 应用程序连接到 WCF 服务
【发布时间】:2019-08-17 03:33:51
【问题描述】:

我昨天开始了一个 POC 项目以连接到现有的 WCF 服务,我正在使用 .Net Core 2.1 Web API 应用程序和 Swagger 来发布我的测试请求消息。我正在为 BasicHttpSecurityMode 使用 BasicHttpBinding 和 Transport。

在发送测试请求消息时,我收到以下异常:

“HTTP 请求未使用客户端身份验证方案‘匿名’。从服务器接收到的身份验证标头是‘基本领域’。”

然后,我使用 .NET Framework 4.6.1 创建了一个 Microsoft Web API 项目来调用 WCF 服务,并且能够获得状态代码 200 HTTP 响应。

在我看来,这像是 .NET Core 2 Web API 项目连接到 WCF 服务的问题。我做了一些研究,看起来这种设计架构绝对是可能的。

微软甚至开发了一个提供者工具来做到这一点,这里是关于他们的提供者工具的 MS 文章的链接:https://docs.microsoft.com/en-us/dotnet/core/additional-tools/wcf-web-service-reference-guide

我什至尝试让我的 .NET Core Web API 控制器调用我在 .NET 4.6.1 类库项目中构建的处理程序类,但无济于事,我仍然收到“HTTP 请求未经客户端身份验证方案授权'基本的'。从服务器收到的身份验证标头是“基本领域”。”例外。

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
        {
            if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_IInsuranceService))
            {
                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;
                result.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;
                return result;
            }
            throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
        }

        private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration)
        {
            if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_IInsuranceService))
            {
                return new System.ServiceModel.EndpointAddress("https://myservicebaseURL\myservice.svc");
            }
            throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
        }

【问题讨论】:

  • Basic认证需要配置result.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Basic;并确保this.ChannelFactory.Credentials.UserName.UserName = "yourusername"; this.ChannelFactory.Credentials.UserName.Password = "yourpassword";提供正确的用户名和密码
  • 感谢您的帮助。不幸的是,我已将您上面列出的配置设置添加到我的服务的 Reference 类并设置了用户名和密码凭据,但我仍然遇到相同的异常。这可能是 Visual Studio 在 IISExpress 中启动应用程序的问题吗?
  • 以下是我的 launchSettings.json 文件中的设置: { "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iis": { "applicationUrl": "localhost/mywebapi" , "sslPort": 0 }, "iisExpress": { "applicationUrl": "localhost:63143", "sslPort": 44331 } },
  • 您是如何创建 WCF 服务的?是否有任何演示可以重现您的问题?

标签: .net wcf asp.net-core asp.net-core-webapi


【解决方案1】:

感谢大家的意见。按照article 中详述的步骤,我能够解决我的问题。

请阅读下面的更新代码以从我的 .NET Core 2 Web Api 调用 WCF 服务:

 public async Task<DataFetchServiceResponseUsingPatient> FetchEntity(String ID)
    {
      var userName = _authenticationSettings.Value.UserName;
      var password = _authenticationSettings.Value.Password;
      var url = _authenticationSettings.Value.Url;

      var basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);

      basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
      ChannelFactory<IMyService> factory = null;
      factory = new ChannelFactory<IMyService>(basicHttpBinding, new EndpointAddress(new Uri(url)));
      factory.Credentials.UserName.UserName = userName;
      factory.Credentials.UserName.Password = password;
      var serviceProxy = factory.CreateChannel();
      ((ICommunicationObject)serviceProxy).Open();
      var opContext = new OperationContext((IClientChannel)serviceProxy);
      var prevOpContext = OperationContext.Current;
      OperationContext.Current = opContext;

      var result = await serviceProxy.MyWCFServiceMethod(ID);

      //Cleanup Factory Objects
      factory.Close();
      ((ICommunicationObject)serviceProxy).Close();
      return result;    
    }

【讨论】:

    猜你喜欢
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 2014-10-28
    相关资源
    最近更新 更多