我有一个第三方WSDL服务,我需要将其集成到.NET Core 2.1 Webapp中。
我通过“添加连接的服务”>“ Microsoft WCF Web服务参考提供程序”添加了服务。
我有一些实用程序类,可以像这样配置服务:
using (RSA rsa = RSA.Create())
{
rsa.ImportParameters(rsaParameters);
using (X509Certificate2 caSigned = GetCASignedCert(rsa))
{
// Yes, this value can outlive both usings
return caSigned.CopyWithPrivateKey(rsa);
}
}
其中
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;
}
}
在用于连接第三方服务的旧版.NET Framework 4.0示例中,有一个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"'.
对其绑定进行了一些配置。他们看起来像这样:
app.config
要使这项工作有效,我需要添加什么到项目中?
答案 0 :(得分: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
。