Good afternoon, I'm developing an API with ASP.NET Core, communicating with Sefaz to send the NF-e, however I'm having a problem signing the XML, below is the code that makes the signature:
var listas = xmlAssinar.GetElementsByTagName(infosTipoOperacao.TagAssinatura);
foreach (XmlNode nodes in listas)
{
foreach (XmlNode childNodes in nodes.ChildNodes)
{
if (!childNodes.Name.Equals(infosTipoOperacao.TagAtributoId))
{
continue;
}
// Cria uma referência para ser assinado
var reference = new Reference
{
DigestMethod = "http://www.w3.org/2000/09/xmldsig#sha1",
Uri = string.Empty
};
// Pega o uri que deve ser assinada
var childElemen = (XmlElement)childNodes;
if (childElemen.GetAttributeNode("Id") != null)
{
reference.Uri = string.Format("#{0}", childElemen.GetAttributeNode("Id").Value);
}
else if (childElemen.GetAttributeNode("id") != null)
{
reference.Uri = string.Format("#{0}", childElemen.GetAttributeNode("id").Value);
}
// Cria um objeto SignedXml
var signedXml = new SignedXml(xmlAssinar)
{
// Adicione a chave ao documento SignedXml
SigningKey = certificadoDigital.PrivateKey
};
// Adiciona uma transformação envelopada à referência
var env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
var c14 = new XmlDsigC14NTransform();
reference.AddTransform(c14);
// Adiciona a referência ao objeto SignedXml
signedXml.AddReference(reference);
// Cria um novo objeto KeyInfo
var keyInfo = new KeyInfo();
// Carrega o certificado em um objeto KeyInfoX509Data e adicione-o ao objeto KeyInfo
keyInfo.AddClause(new KeyInfoX509Data(certificadoDigital));
// Adiciona o objeto KeyInfo ao objeto SignedXml
signedXml.KeyInfo = keyInfo;
signedXml.ComputeSignature();
// Obtém a representação XML da assinatura e salve-a em um objeto XmlElement
var xmlDigitalSignature = signedXml.GetXml();
// Grava o elemento no documento XML
nodes.AppendChild(xmlAssinar.ImportNode(xmlDigitalSignature, true));
}
}
xmlAssinar.PreserveWhitespace = false;
return xmlAssinar;
This code I also use in the ASP.NET Framework and it works normally, however when using Core, in the validation of NFe I get the following error:
The value of the 'Algorithm' attribute does not equal its fixed value
or
The value of the 'Algorithm' attribute is not equal to its fixed value
It was then that I realized that within the "PrivateKey" object in the Digital Certificate has a different return from the Framework return and I believe this is affecting the signature algorithm.
If anyone has any suggestions, I'll be grateful right away.