Principales respuestas
Firmar archivo XML

Pregunta
-
Necesito firmar un documento XML, tengo el siguiente codigo:
using System; using System.Security.Cryptography; using System.Xml; namespace FirmaXML { class Program { static void Main(string[] args) { try { // Create a new CspParameters object to specify a key container. CspParameters cspParams = new CspParameters(); cspParams.KeyContainerName = "XML_DSIG_RSA_KEY"; // Create a new RSA signing key and save it in the container. RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams); // Create a new XML document. XmlDocument xmlDoc = new XmlDocument(); // Load an XML file into the XmlDocument object. xmlDoc.PreserveWhitespace = true; xmlDoc.Load("test.xml"); // Sign the XML document. SignXml(xmlDoc, rsaKey); Console.WriteLine("XML file signed."); // Save the document. xmlDoc.Save("test.xml"); } catch (Exception e) { Console.WriteLine(e.Message); } } 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. 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)); } } }
Pero en las lineas del metodo tengo errores en las siguientes lineas:
// Create a SignedXml object.
SignedXml signedXml = new SignedXml(xmlDoc);
// Add the key to the SignedXml document.
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);alguien sabe el porque de estos errores, o como resolverlos.
Gracias de antemano.
Respuestas
-
Probablemente te falta el "using System.Security.Cryptography.Xml", o la correspondiente Referencia a System.Security.dll en el proyecto.
- Propuesto como respuesta Jorge TurradoMVP martes, 14 de noviembre de 2017 14:11
- Marcado como respuesta aus2302 miércoles, 15 de noviembre de 2017 13:09
Todas las respuestas
-
hola
>>Pero en las lineas del metodo tengo errores en las siguientes lineas
que dice los mensajes de error?
son errores cuando compilas o cuando ejecutas?
saludos
Leandro Tuttini
Blog
MVP Profile
Buenos Aires
Argentina -
Probablemente te falta el "using System.Security.Cryptography.Xml", o la correspondiente Referencia a System.Security.dll en el proyecto.
- Propuesto como respuesta Jorge TurradoMVP martes, 14 de noviembre de 2017 14:11
- Marcado como respuesta aus2302 miércoles, 15 de noviembre de 2017 13:09
-
-