正在尝试将ASP.NET CORE 2 Web API应用程序连接到WCF服务
我昨天开始了一个POC项目,连接到一个现有的WCF服务,我正在使用.NETCore2.1WebAPI应用程序和Swagger发布我的测试请求消息。我正在为BasicHttpSecurityMode使用BasicHttpBinding和Transport 在发送测试请求消息时,我收到以下异常: “HTTP请求未经客户端身份验证方案‘匿名’授权。从服务器接收的身份验证标头为‘基本域’。” 然后,我使用.NETFramework4.6.1创建了一个MicrosoftWebAPI项目来调用WCF服务,并获得了状态码200HTTP响应 在我看来,这似乎是连接到WCF服务的.NETCore2WebAPI项目的一个问题。我做了一些研究,看起来这种设计架构肯定是可能的 Microsoft甚至开发了一个提供程序工具来实现这一点,以下是有关其提供程序工具的MS文章的链接: 我甚至尝试让我的.NET Core Web API控制器调用我在.NET 4.6.1类库项目中构建的处理程序类,但没有成功,我仍然得到“HTTP请求未经客户端身份验证方案'Basic'授权”。从服务器接收的身份验证头是'Basic Realm'。异常正在尝试将ASP.NET CORE 2 Web API应用程序连接到WCF服务,.net,wcf,asp.net-core,asp.net-core-webapi,.net,Wcf,Asp.net Core,Asp.net Core Webapi,我昨天开始了一个POC项目,连接到一个现有的WCF服务,我正在使用.NETCore2.1WebAPI应用程序和Swagger发布我的测试请求消息。我正在为BasicHttpSecurityMode使用BasicHttpBinding和Transport 在发送测试请求消息时,我收到以下异常: “HTTP请求未经客户端身份验证方案‘匿名’授权。从服务器接收的身份验证标头为‘基本域’。” 然后,我使用.NETFramework4.6.1创建了一个MicrosoftWebAPI项目来调用WCF服务,并
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));
}
谢谢大家的意见。我能够按照本文中详述的步骤解决我的问题 请阅读下面的更新代码,以便从.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;
}
公共异步任务获取实体(字符串ID)
{
var userName=\u authenticationSettings.Value.userName;
var password=\u authenticationSettings.Value.password;
var url=\u authenticationSettings.Value.url;
var basicHttpBinding=新的basicHttpBinding(BasicHttpSecurityMode.Transport);
basicHttpBinding.Security.Transport.ClientCredentialType=HttpClientCredentialType.Basic;
ChannelFactory=null;
factory=newchannelfactory(basicHttpBinding,新端点地址(新Uri(url));
factory.Credentials.UserName.UserName=用户名;
factory.Credentials.UserName.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);
//清理工厂对象
工厂关闭();
((ICommunicationObject)serviceProxy.Close();
返回结果;
}
对于Basic
身份验证,您需要配置result.Security.Transport.ClientCredentialType=System.ServiceModel.HttpClientCredentialType.Basic
并确保通过this.ChannelFactory.Credentials.username.username=“yourusername”提供正确的用户名和密码;this.ChannelFactory.Credentials.UserName.Password=“yourspassword”代码>谢谢你的帮助。不幸的是,我已经将上面列出的配置设置添加到了我的服务的引用类中,并设置了用户名和密码凭据,但我仍然得到相同的异常。这可能是Visual Studio在IISExpress中如何启动应用程序的问题吗?以下是我的launchSettings.json文件中的设置:{“iisSettings”:{“windowsAuthentication”:false,“anonymousAuthentication”:true,“iis”:{“applicationUrl”:“,“sslPort”:0},“IISExpress”:{“applicationUrl”:”,“sslPort”:44331}},您是如何创建WCF服务的?是否有任何演示重现您的问题?