signieren eines xml-Dokuments mit x509-Zertifikat

Jedes mal, wenn ich versuche, das senden einer signierten XML, die web service verifier lehnt Sie ab.

Unterzeichnen das Dokument, das ich einfach nur angepasst dieser Beispielcode von Microsoft zur Verfügung:

http://msdn.microsoft.com/es-es/library/ms229745(v=vs. 110).aspx

Meine Umsetzung:

    public static XmlDocument FirmarXML(XmlDocument xmlDoc)
    {
        try
        {
            X509Certificate2 myCert = null;
            var store = new X509Store(StoreLocation.CurrentUser); //StoreLocation.LocalMachine fails too
            store.Open(OpenFlags.ReadOnly);
            var certificates = store.Certificates;
            foreach (var certificate in certificates)
            {
                if (certificate.Subject.Contains("xxx"))
                {
                    myCert = certificate;
                    break;
                }
            }

            if (myCert != null)
            {
                RSA rsaKey = ((RSA)myCert.PrivateKey);

                //Sign the XML document. 
                SignXml(xmlDoc, rsaKey);                    
            }

        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
        return xmlDoc;
    }


    //Sign an XML file. 
    //This document cannot be verified unless the verifying 
    //code has the key with which it was signed.
    public static void SignXml(XmlDocument xmlDoc, RSA Key)
    {
        //Check arguments.
        if (xmlDoc == null)
            throw new ArgumentException("xmlDoc");
        if (Key == null)
            throw new ArgumentException("Key");

        //Create a SignedXml object.
        SignedXml signedXml = new SignedXml(xmlDoc);

        //Add the key to the SignedXml document.
        signedXml.SigningKey = Key;

        //Create a reference to be signed.
        Reference reference = new Reference();
        reference.Uri = "";

        //Add an enveloped transformation to the reference.
        XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
        reference.AddTransform(env);

        //Add the reference to the SignedXml object.
        signedXml.AddReference(reference);

        //Compute the signature.
        signedXml.ComputeSignature();

        //Get the XML representation of the signature and save
        //it to an XmlElement object.
        XmlElement xmlDigitalSignature = signedXml.GetXml();

        //Append the element to the XML document.
        xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));

    }

Ich glaube, ich bin die gleichen Schritte mit meinem eigenen Zertifikat, aber es funktioniert nicht wie erwartet.

Jede Anregung willkommen sein wird.

Schreibe einen Kommentar