I develop an application where I sign an XML document, in the following way, but I have to add the signature policy (Certificate provided by the company) and I don't know how to do it. This is how I sign the XML document:
public string CreateCspParameters (string path)
{
error = "true";
try
{
CspParameters cspParams = new CspParameters();
cspParams.KeyContainerName = "XML_DSIG_RSA_KEY";
RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load(path);
SignXml(xmlDoc, rsaKey);
xmlDoc.Save(path);
}
catch (Exception ex) { error = ex.ToString(); }
return error;
}
public void SignXml(XmlDocument xmlDoc, RSA Key)
{
if (xmlDoc == null)
throw new ArgumentException("xmlDoc");
if (Key == null)
throw new ArgumentException("Key");
SignedXml signedXml = new SignedXml(xmlDoc);
signedXml.SigningKey = Key;
Reference reference = new Reference();
reference.Uri = "";
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
signedXml.AddReference(reference);
signedXml.ComputeSignature();
XmlElement xmlDigitalSignature = signedXml.GetXml();
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
}
This way I can choose the certificate provided by the company but I don't know how to add it to the previous SignXml() method to complete the Xade-Epes
public X509Certificate2Collection ElejirCertificado()
{
X509Store store = new X509Store(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection certificates = store.Certificates;
X509Certificate2Collection foundCertificates = certificates.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
X509Certificate2Collection selectedCertificates = X509Certificate2UI.SelectFromCollection(foundCertificates,
"Selecciona un certificado.",
"Selecciona un certificado de la siguiente lista:",
X509SelectionFlag.SingleSelection);
return selectedCertificates;
}
1 Answers