【问题标题】:c# MessageSecurityException in a web servicec#Web服务中的MessageSecurityException
【发布时间】:2020-09-30 14:57:29
【问题描述】:

我有一个使用多年的网络服务功能。今天他们给我发了一个新证书,一个 .cer 文件,我插入了它而不是旧的,但是我收到了这个错误: (用谷歌翻译,对不起)

MessageSecurityException 
Failed to complete identity check for outgoing message.
The expected DNS identity of the remote endpoint was 'pddasl-coll.rmmg.rsr.rupar.puglia.it'
but the remote endpoint provided a DNS claim 'pdd-virtasl.rmmg.rsr.rupar.puglia.it'.
If this is a legitimate remote endpoint, you can fix the problem by specifying
explicitly the DNS identity 'pdd-virtasl.rmmg.rsr.rupar.puglia.it'
as the Identity property of EndpointAddress when creating the channel proxy.

我询问了网络服务的所有者,他们告诉我必须确保忽略该错误,但我不知道该怎么做。我尝试在 app.config 中插入:enableUnsecuredResponse = "true" 但它不起作用。

这是连接网络服务的方法:

    public static CVPClient Connect()
    {
        CVPClient oConsist = null;
        string cEndPoint = "https://pddasl-coll.rmmg.rsr.rupar.puglia.it:8181/aslba/CVPService";
        ServicePointManager.ServerCertificateValidationCallback = Leo.CertificateHandler;
        datiOperatore DataOp = Leo.OperatorData();//unimportant parameters
        datiApplicativo DataApp = Leo.AppData();//unimportant parameters
        var b = new CustomBinding();
        var sec = new AsymmetricSecurityBindingElement(
            new X509SecurityTokenParameters(X509KeyIdentifierClauseType.Any, SecurityTokenInclusionMode.Never),
            new X509SecurityTokenParameters(X509KeyIdentifierClauseType.Any, SecurityTokenInclusionMode.AlwaysToRecipient));
        sec.MessageSecurityVersion = MessageSecurityVersion.WSSecurity10WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10;
        sec.SecurityHeaderLayout = SecurityHeaderLayout.Strict;
        sec.IncludeTimestamp = true;
        sec.SetKeyDerivation(false);
        sec.KeyEntropyMode = System.ServiceModel.Security.SecurityKeyEntropyMode.ServerEntropy;
        sec.EnableUnsecuredResponse = true;
        b.Elements.Add(sec);
        b.Elements.Add(new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8));
        b.Elements.Add(new HttpsTransportBindingElement());
        EndpointAddress ea = new EndpointAddress(cEndPoint);
        oConsist = new CVPClient(b, ea);
        X509Certificate2 certSigned = Leo.GetSignedCert();//this returns my private certificate, not the one they replaced me

        string cPin = "123456";
        System.Security.SecureString SecurePIN = new System.Security.SecureString();
        foreach (char ch in cPin)
        { SecurePIN.AppendChar(ch); }
        var rsa = (RSACryptoServiceProvider)certSigned.PrivateKey;
        string ContinerName = rsa.CspKeyContainerInfo.KeyContainerName;
        string CspName = rsa.CspKeyContainerInfo.ProviderName;
        int CspType = rsa.CspKeyContainerInfo.ProviderType;
        CspParameters csp = new CspParameters(CspType, CspName, ContinerName, new System.Security.AccessControl.CryptoKeySecurity(), SecurePIN);
        RSACryptoServiceProvider CSP = new RSACryptoServiceProvider(csp);

        X509Certificate2 certUnsigned = Leo.GetUnSignedCertificate();//Here I read the new certificate

        oConsist.ClientCredentials.ClientCertificate.Certificate = certSigned;
        oConsist.ClientCredentials.ServiceCertificate.DefaultCertificate = certUnsigned;
        oConsist.Open();

        return oConsist;
    }

你也可以在这里看到我插入了 sec.EnableUnsecuredResponse = true;连接成功,但正如我之前所说,当我调用 Web 服务时出现错误。 我该如何解决这个问题?

更新: 由于错误告诉我明确分配 DNS 身份“pdd-virtasl.rmmg.rsr.rupar.puglia.it” 作为 EndpointAddress 的 Identity 属性,我替换了这一行:

EndpointAddress ea = new EndpointAddress(cEndPoint);

用这个:

DnsEndpointIdentity identity = new DnsEndpointIdentity(cEndPoint);
EndpointAddress ea = new EndpointAddress(new Uri(cEndPoint), identity, new AddressHeaderCollection());

但它不起作用,我得到同样的错误

【问题讨论】:

  • 您未能通过 TLS 身份验证。您使用的加密模式必须与 TLS 版本兼容(请参阅en.wikipedia.org/wiki/Transport_Layer_Security)。由于安全问题,该行业五年前决定取消 TLS 1.0/1.1。微软今年 6 月在服务器上推送了禁用 TLS 1.0/1.1 的安全更新,并要求客户端仅请求 TLS 1.2/1.3。未在 c# 代码中指定时使用的默认 TLS 版本取决于 VS 版本和 windows 版本。我会使用嗅探器来确定正在使用的 TLS 版本。

标签: c# web-services endpoint


【解决方案1】:

正确的解决办法是这样的:

            DnsEndpointIdentity identity = new DnsEndpointIdentity("pdd-virtasl.rmmg.rsr.rupar.puglia.it");
            EndpointAddress ea = new EndpointAddress(new Uri(cEndPoint), identity, new AddressHeaderCollection());

您必须使用 Web 服务端点,但在身份中指定证书中包含的端点。

【讨论】:

    猜你喜欢
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多