www.example.com core 2.0中的WCFasp.net-无法为SSL/TLS安全通道与授权机构建立信任关系

ryevplcw  于 2023-02-17  发布在  .NET
关注(0)|答案(2)|浏览(170)

以前我有一个应用程序目标框架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

我很感激你们的帮助。我错过什么了吗?

z31licg0

z31licg01#

如果您使用不受信任的服务器证书(例如自签名证书),您应该使用:

client.ClientCredentials.ServiceCertificate.SslCertificateAuthentication =
        new X509ServiceCertificateAuthentication()
        {
            CertificateValidationMode = X509CertificateValidationMode.None,
            RevocationMode = System.Security.Cryptography.X509Certificates.X509RevocationMode.NoCheck
        };
rqdpfwrv

rqdpfwrv2#

请尝试以下操作:

var httpBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport)
{
    MaxReceivedMessageSize = 999999,
};
httpBinding.Security.Transport = new HttpTransportSecurity
{
    ClientCredentialType = HttpClientCredentialType.Certificate
};

上述http设置等效于连接到WCF时在. NET Framework中的webconfig中生成的"System. serviceModel" xml。

<bindings>
    <basicHttpBinding>
        <binding name="myBindingConfiguration1"
                 maxReceivedMessageSize="999999">
            <security mode="Transport">
                <transport clientCredentialType="Certificate"/>
            </security>
        </binding>
    </basicHttpBinding>
</bindings>

相关问题