I ended up having the same problem about a post already done here, however come a doubt next to what was posted in the article. What windows update would be to remove from the ones mentioned in this article ? (removal would be a palliative until the problem is solved)
Out of doubt about the article, follow the code that I'm having problems with.
public void AssinarElementos(XmlDocument Document, X509Certificate2 x509, string ParentElementName, string ElementName, string AttributeName)
{
XmlElement elInf;
string elInfID;
SignedXml elSigned;
RSACryptoServiceProvider Key;
KeyInfo keyInfo = new KeyInfo();
// Retira chave privada ligada ao certificado
Key = ((RSACryptoServiceProvider)(x509.PrivateKey));
keyInfo.AddClause(new KeyInfoX509Data(x509));
foreach (XmlElement ele in Document.GetElementsByTagName(ParentElementName))
{
elInf = ((XmlElement)(ele.GetElementsByTagName(ElementName)[(ele.GetElementsByTagName(ElementName).Count - 1)]));
elInfID = elInf.Attributes.GetNamedItem(AttributeName).Value;
elSigned = new SignedXml(elInf);
// Seta chaves
elSigned.SigningKey = Key;
elSigned.KeyInfo = keyInfo;
// Cria referencia
Reference reference = new Reference();
reference.Uri = ("#" + elInfID);
// Adiciona tranformacao a referencia
reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
reference.AddTransform(new XmlDsigC14NTransform(false));
// Adiciona referencia ao xml
elSigned.AddReference(reference);
// Calcula Assinatura
elSigned.ComputeSignature();
// Adiciona assinatura
ele.AppendChild(Document.ImportNode(elSigned.GetXml(), true));
}
}
A Elemento de referência mal formado.
error is generated on the line where elSigned.ComputeSignature();
will run (I ran a test on a machine with windows 10 and it worked perfectly).