Asp.net 使用数字签名证书生成XML文件

Asp.net 使用数字签名证书生成XML文件,asp.net,xml,vb.net,digital-signature,xml-signature,Asp.net,Xml,Vb.net,Digital Signature,Xml Signature,我正试图用OpenSSL生成的私有RSA密钥在C#.NET 4.0中对XML文件进行签名。 我的源代码如下所示: public static void SignXml(String filePath, String certificatePath) { CspParameters cspParams1 = new CspParameters(); cspParams1.KeyContainerName = certificatePath;

我正试图用OpenSSL生成的私有RSA密钥在C#.NET 4.0中对XML文件进行签名。 我的源代码如下所示:

    public static void SignXml(String filePath, String certificatePath)
    {
        CspParameters cspParams1 = new CspParameters();
        cspParams1.KeyContainerName = certificatePath;
        RSACryptoServiceProvider rsakey = new RSACryptoServiceProvider(cspParams1);


        XmlDocument xmlDoc = new XmlDocument();

        // Load an XML file into the XmlDocument object.
        xmlDoc.PreserveWhitespace = true;
        xmlDoc.Load(filePath);
        SignedXml signedXml = new SignedXml();
        CspParameters cspParams = new CspParameters();
        cspParams.KeyContainerName = certificatePath;

        // Create a new RSA signing key and save it in the container. 
        RSACryptoServiceProvider Key = new RSACryptoServiceProvider(cspParams);

        // 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);
        KeyInfo keyInfo = new KeyInfo();

        // Load the X509 certificate.


        X509Certificate MSCert = X509Certificate.CreateFromCertFile(certificatePath);


        // Load the certificate into a KeyInfoX509Data object 
        // and add it to the KeyInfo object.
        keyInfo.AddClause(new KeyInfoX509Data(MSCert));
        keyInfo.AddClause(new RSAKeyValue((RSA)Key));

        // Add the KeyInfo object to the SignedXml object.
        signedXml.KeyInfo = keyInfo;
        // 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));


        xmlDoc.Save(filePath);
    }
我正在以DLL(名为DBTBeneficiariesCPSMS)的形式调用应用程序中的类(CPSMSXmlGenerator),调用该类的代码为:

  Dim genXml As String = DBTBeneficiariesCPSMS.CPSMSXmlGenerator.getXmlFile1(xml)

        'Dim appPath As String = Request.PhysicalApplicationPath
        Dim fullPath As String = Server.MapPath("/XML/") + dataSource + ".xml"
        lblMessage.Text = fullPath
        Dim SwFromFile As StreamWriter = New StreamWriter(fullPath)
        SwFromFile.Write(genXml)
        SwFromFile.Flush()
        SwFromFile.Close()

        CPSMSXmlGenerator.SignXml(fullPath, Server.MapPath("/XML/aua.cer"))
现在,问题是每当我的应用程序运行时,它都会在“Reference.Uri=”“”处停止,并出现如下错误:- 错误:解析引用Uri需要XmlDocument上下文


显示,并生成不带数字签名证书的XML文件。

xmDoc不会传递给SignedXml。将其作为param传递应该可以解决问题

SignedXml signedXml = new SignedXml(xmlDoc); 

以防万一有人来了:对我来说,发生错误是因为我没有将Uri设置为空字符串。