Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.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#发送带有X.509证书签名的SOAP消息_C#_Web Services_Ssl_Soap_Soapui - Fatal编程技术网

C#发送带有X.509证书签名的SOAP消息

C#发送带有X.509证书签名的SOAP消息,c#,web-services,ssl,soap,soapui,C#,Web Services,Ssl,Soap,Soapui,我试图向需要X.509证书的SOAP服务发送消息 在中,我在中设置证书路径和密码,在请求窗口中输入信封,点击提交并获得结果 在C#中,它变得复杂了,主要是因为我对这些东西非常陌生。因此,首先我尝试使用guide编写一个简单的消息发送方法,它很有效 下一步是(我猜)用证书对我的SOAP消息进行签名,这方面也有一个指南,至少有一个指南,所以现在我有了一个可以发送SOAP消息的类/方法和一个可以对SOAP消息进行签名的类/方法 但我不知道如何实际签名,我的代码如下所示: public class SO

我试图向需要X.509证书的SOAP服务发送消息

在中,我在中设置证书路径和密码,在请求窗口中输入信封,点击提交并获得结果

在C#中,它变得复杂了,主要是因为我对这些东西非常陌生。因此,首先我尝试使用guide编写一个简单的消息发送方法,它很有效

下一步是(我猜)用证书对我的SOAP消息进行签名,这方面也有一个指南,至少有一个指南,所以现在我有了一个可以发送SOAP消息的类/方法和一个可以对SOAP消息进行签名的类/方法

但我不知道如何实际签名,我的代码如下所示:

public class SOAPTest
{
    public static string Call(string argsUrl, string requestAction, string argsXml)
    {
        Uri destination_uri = new Uri(argsUrl);
        EndpointReference destination = new EndpointReference(destination_uri);
        SoapEnvelope envelope = new SoapEnvelope();

        envelope.LoadXml(argsXml);

        // The destination variable is the EndpointReference 
        // for the Web service.
        TcpClient client = new TcpClient(destination);

        SoapEnvelope returnEnvelope = client.RequestResponseMethod(envelope);
        // The envelope that is returned is the response data.

        return returnEnvelope.OuterXml;
    }
}
我有和中提到的所有类:CustomSecurityAssertion、CustomSecurityServerInputFilter、CustomSecurityServerOutputFilter、CustomSecurityClientInputFilter和CustomSecurityClientOutputFilter,最后一个看起来像这样,据我所知,我需要使用
SecureMessage
方法对信封进行签名:

class CustomSecurityClientOutputFilter : SendSecurityFilter
{
    SecurityToken clientToken;
    SecurityToken serverToken;

    public CustomSecurityClientOutputFilter(CustomSecurityAssertion parentAssertion)
        : base(parentAssertion.ServiceActor, true)
    {
        // Get the client security token.
        clientToken = X509TokenProvider.CreateToken(StoreLocation.CurrentUser, StoreName.My, "CN=B2B");

        // Get the server security token.
        serverToken = X509TokenProvider.CreateToken(StoreLocation.LocalMachine, StoreName.My, "CN=B2B");
    }

    public override void SecureMessage(SoapEnvelope envelope, Security security)
    {
        X509SecurityToken signatureToken = GetSecurityToken("CN=B2B");
        if (signatureToken == null)
        {
            throw new Exception("Message Requirements could not be satisfied.");
        }

        // Add the security token.                
        security.Tokens.Add(signatureToken);
        // Specify the security token to sign the message with.
        MessageSignature sig = new MessageSignature(signatureToken);

        security.Elements.Add(sig);

    }

    public X509SecurityToken GetSecurityToken(string subjectName)
    {
        X509SecurityToken securityToken = null;
        X509Store store = new X509Store(StoreName.My,
          StoreLocation.CurrentUser);
        store.Open(OpenFlags.ReadOnly);
        try
        {
            X509Certificate2Collection certs =
                store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName,
                subjectName, false);

            X509Certificate2 cert;
            if (certs.Count == 1)
            {
                cert = certs[0];
                securityToken = new X509SecurityToken(cert);
            }
            else
                securityToken = null;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        finally
        {
            if (store != null)
                store.Close();
        }
        return securityToken;
    }
}
我如何“连接”这两者,并获得在Soap UI中得到的相同结果

我是否至少做了正确的事情来签署这条信息?如果我尝试按原样运行项目,则会收到以下错误消息:

请求被中止:无法创建SSL/TLS安全通道

但如果我提供了错误的证书名称,则会出现错误:

基础连接已关闭:发送时发生意外错误

因此,看起来SOAP消息签名可以工作,但服务器仍然拒绝连接,但是使用SOAP UI和相同的证书以及相同的SOAP信封,一切都可以正常工作,没有问题,有什么建议吗?

类似的问题: