Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用x509证书在wcf中进行消息签名_C#_Asp.net_Wcf_X509certificate - Fatal编程技术网

C# 使用x509证书在wcf中进行消息签名

C# 使用x509证书在wcf中进行消息签名,c#,asp.net,wcf,x509certificate,C#,Asp.net,Wcf,X509certificate,我在尝试使用WCF web服务时遇到了一个问题,该服务需要使用X509证书进行相互身份验证和消息签名。我已经使用X509证书实现了相互身份验证,但在尝试实现消息签名时遇到了一个问题。我已在我的计算机上成功安装了证书。我收到的错误消息是: 签名验证失败 请注意,我已在SoapUI中成功测试了此应用程序。但我在尝试在C#中实现同样的功能时面临着这个问题 我的代码: public override void SecureMessage(SoapEnvelope envelope, Security s

我在尝试使用WCF web服务时遇到了一个问题,该服务需要使用X509证书进行相互身份验证和消息签名。我已经使用X509证书实现了相互身份验证,但在尝试实现消息签名时遇到了一个问题。我已在我的计算机上成功安装了证书。我收到的错误消息是:

签名验证失败

请注意,我已在SoapUI中成功测试了此应用程序。但我在尝试在C#中实现同样的功能时面临着这个问题

我的代码:

public override void SecureMessage(SoapEnvelope envelope, Security security)
{
        // Get an X.509 certificate for signing the SOAP message.
        X509SecurityToken signatureToken = GetSecurityToken("subjectname");

        if (signatureToken == null)
        {
            throw new SecurityFault("Message Requirements could not be satisfied.");
        }

        // Add the X.509 certificate to the header.
        security.Tokens.Add(signatureToken);

        // Specify that the SOAP message is signed using this X.509
        // certificate.
        MessageSignature sig = new MessageSignature(signatureToken);
        security.Elements.Add(sig);
    }

    public X509SecurityToken GetSecurityToken(string subjectName)
    {
        X509SecurityToken objX509SecurityToken = null;
        X509Store objX509Store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
        objX509Store.Open(OpenFlags.ReadOnly);

        try
        {
            X509Certificate2Collection objX509Certificate2Collection = objX509Store.Certificates.Find(X509FindType.FindBySubjectName, subjectName, true);

            X509Certificate2 objX509Certificate2;

            if (objX509Certificate2Collection.Count == 1)
            {
                objX509Certificate2 = objX509Certificate2Collection[0];
                objX509SecurityToken = new X509SecurityToken(objX509Certificate2);
            }
            else
            {
                objX509SecurityToken = null;
            }
        }
        catch (Exception ex)
        {
            objX509SecurityToken = null;
        }
        finally
        {
            if (objX509Store != null)
                objX509Store.Close();
        }

        return objX509SecurityToken;
    }

在任何人开始帮助之前,你至少需要显示一些代码……马克-我已经编辑了这个问题,上面是我用于消息签名的代码。我需要知道如何将这个令牌添加到我的web服务请求soap头中。我已经解决了这个问题。如果有人面临同样的问题,以下是参考链接: