以前我有一个应用程序目标框架4.5.1,并使用Web引用添加一个WCF服务。这工作完美,并能够成功地与服务器进行身份验证。
旧代码:
ServicePointManager.Expect100Continue = false;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
IService orderService = new Service();
// private method to load certificate
var cert = LoadCertificate(@"....\00050._.1.p12", "pwd");
oIPGApiOrderService.ClientCertificates.Add(cert);
oIPGApiOrderService.Url = @"service_url";
NetworkCredential nc = new NetworkCredential("username", "pwd");
oIPGApiOrderService.Credentials = nc;
现在我正在升级到.net核心2.0,目标是.net标准2.0。我已经添加了使用服务引用(连接服务)的服务。生成的代码已经改变,使用通道工厂等...
新代码:
ServicePointManager.Expect100Continue = false;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
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;
IService client = new Service(result, new EndpointAddress("service_url"));
client.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2(@"....\00050._.1.p12", "pwd");
client.ClientCredentials.UserName.UserName = "username";
client.ClientCredentials.UserName.Password = "pwd";
但新代码不工作,虽然证书加载成功。我得到一个异常:
{System.ServiceModel.Security.SecurityNegotiationException: Could not establish trust relationship for the SSL/TLS secure channel with authority 'xxxxxxx-online.com'. ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.WinHttpException: A security error occurred
我很感激你们的帮助。我错过什么了吗?
2条答案
按热度按时间z31licg01#
如果您使用不受信任的服务器证书(例如自签名证书),您应该使用:
rqdpfwrv2#
请尝试以下操作:
上述http设置等效于连接到WCF时在. NET Framework中的webconfig中生成的"System. serviceModel" xml。