我有一个 .NET Framework 4.8 项目,它正在调用 .NET 4.8 WCF 服务。我想使用 https,但现在我只有自签名证书,因此我只是忽略所有验证错误:
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true;
var binding = new BasicHttpBinding()
{
SendTimeout = TimeSpan.FromMinutes(10),
MaxReceivedMessageSize = Int32.MaxValue,
};
binding.Security.Mode = BasicHttpSecurityMode.Transport;
using (var client = new SampleService(binding, new EndpointAddress("https://localhost/SampleService/Service.svc")))
{
var response = client.GetConfiguration();
}
一切正常,但现在我使用 .NET 6.0 创建了另一个项目,该项目调用相同的 WCF 服务。现在,我收到错误:
无法与权限“localhost”建立 SSL/TLS 安全通道的信任关系。
看起来像这位代表:
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true;
永远不会被击中,这会导致问题。做了一些研究后,我尝试了这样的例子:
using (var client = new SampleService(binding, new EndpointAddress("https://localhost/SampleService/Service.svc")))
{
client.ClientCredentials.ServiceCertificate.SslCertificateAuthentication = new System.ServiceModel.Security.X509ServiceCertificateAuthentication()
{
CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None,
RevocationMode = System.Security.Cryptography.X509Certificates.X509RevocationMode.NoCheck
};
var response = client.GetConfiguration();
}
但它需要
System.IdentityModel.dll
,它永远不会复制到输出目录。如果我强制 Visual Studio 这样做,它仍然显示该错误并且找不到此 dll。有什么想法我可以在这里做什么吗?
你可以试试这个方法:
首先,客户端引用服务后会生成一个Reference.cs文件。
这里有这个客户端的构造函数。通常,有多个构造函数。
如:
public Service1Client() :
base(Service1Client.GetDefaultBinding(), Service1Client.GetDefaultEndpointAddress())
{
this.Endpoint.Name = EndpointConfiguration.BasicHttpBinding_IService1.ToString();
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}
public Service1Client(EndpointConfiguration endpointConfiguration) :
base(Service1Client.GetBindingForEndpoint(endpointConfiguration), Service1Client.GetEndpointAddress(endpointConfiguration))
{
this.Endpoint.Name = endpointConfiguration.ToString();
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}
public Service1Client(EndpointConfiguration endpointConfiguration, string remoteAddress) :
base(Service1Client.GetBindingForEndpoint(endpointConfiguration), new System.ServiceModel.EndpointAddress(remoteAddress))
{
this.Endpoint.Name = endpointConfiguration.ToString();
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}
public Service1Client(EndpointConfiguration endpointConfiguration, System.ServiceModel.EndpointAddress remoteAddress) :
base(Service1Client.GetBindingForEndpoint(endpointConfiguration), remoteAddress)
{
this.Endpoint.Name = endpointConfiguration.ToString();
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}
public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{
}
您可以将此代码直接放在构造函数中。
如:
public Service1Client() :
base(Service1Client.GetDefaultBinding(), Service1Client.GetDefaultEndpointAddress())
{
System.Net.ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
this.Endpoint.Name = EndpointConfiguration.BasicHttpBinding_IService1.ToString();
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}