【发布时间】: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