Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Resultado AssinarXml(string arquivo, string tagAssinatura, string tagAtributoId, string serial, string numeroRPS)
- {
- try
- {
- SR = System.IO.File.OpenText(arquivo);
- string xmlString = SR.ReadToEnd();
- SR.Close();
- SR = null;
- XmlDocument doc = new XmlDocument();
- doc.PreserveWhitespace = false;
- doc.LoadXml(xmlString);
- if (doc.GetElementsByTagName(tagAssinatura).Count == 0)
- {
- resultado.Result = false;
- resultado.Mensagem = $"A tag de assinatura {tagAssinatura.Trim()} não existe no XML. (Código do Erro: 5)";
- Console.WriteLine(resultado.Mensagem);
- return resultado;
- //throw new Exception($"A tag de assinatura {tagAssinatura.Trim()} não existe no XML. (Código do Erro: 5)");
- }
- else if (doc.GetElementsByTagName(tagAtributoId).Count == 0)
- {
- resultado.Result = false;
- resultado.Mensagem = $"A tag de assinatura {tagAtributoId.Trim()} não existe no XML. (Código do Erro: 4)";
- Console.WriteLine(resultado.Mensagem);
- return resultado;
- //throw new Exception($"A tag de assinatura {tagAtributoId.Trim()} não existe no XML. (Código do Erro: 4)");
- }
- else
- {
- XmlDocument XMLDoc;
- XmlNodeList lists = doc.GetElementsByTagName(tagAssinatura);
- foreach (XmlNode nodes in lists)
- {
- foreach (XmlNode childNodes in nodes.ChildNodes)
- {
- if (!childNodes.Name.Equals(tagAtributoId))
- continue;
- if (childNodes.NextSibling != null && childNodes.NextSibling.Name.Equals("Signature"))
- continue;
- Reference reference = new Reference("");
- reference.Uri = "";
- XmlElement childElemen = (XmlElement)childNodes;
- if (childElemen.GetAttributeNode("Id") != null)
- {
- reference.Uri = "#" + "RPS" + numeroRPS.ToString().PadLeft(15, '0');
- }
- else if (childElemen.GetAttributeNode("id") != null)
- {
- reference.Uri = "#" + childElemen.GetAttributeNode("id").Value;
- }
- SignedXml signedXml = new SignedXml(doc);
- KeyInfo keyInfo = new KeyInfo();
- signedXml.KeyInfo = keyInfo;
- signedXml.SigningKey = x509Cert.PrivateKey;
- signedXml.SignedInfo.SignatureMethod = SignedXml.XmlDsigRSASHA1Url;
- reference.DigestMethod = SignedXml.XmlDsigSHA1Url;
- XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
- reference.AddTransform(env);
- XmlDsigC14NTransform c14 = new XmlDsigC14NTransform();
- reference.AddTransform(c14);
- signedXml.AddReference(reference);
- keyInfo.AddClause(new KeyInfoX509Data(x509Cert));
- signedXml.KeyInfo = keyInfo;
- signedXml.ComputeSignature();
- XmlElement xmlDigitalSignature = signedXml.GetXml();
- nodes.AppendChild(doc.ImportNode(xmlDigitalSignature, true));
- }
- }
- XMLDoc = new XmlDocument();
- XMLDoc.PreserveWhitespace = false;
- XMLDoc = doc;
- string conteudoXMLAssinado = XMLDoc.OuterXml;
- using (StreamWriter sw = System.IO.File.CreateText(arquivo))
- {
- sw.Write(conteudoXMLAssinado);
- sw.Flush();
- sw.Close();
- }
- resultado.Result = true;
- resultado.Mensagem = $"Procecesso de assinatura efetuado com sucesso!!!";
- Console.WriteLine(resultado.Mensagem);
- return resultado;
- }
- }
- catch (Exception ex)
- {
- //cod = "00000"; msgm = "Certificado não identificado, reinicie o certificado e tente novamente";
- resultado.Result = false;
- resultado.Mensagem = "Código: 00000 - Certificado não identificado, reinicie o certificado e tente novamente";
- Console.WriteLine(ex);
- return resultado;
- //throw new Exception("Código: 00000 - Certificado não identificado, reinicie o certificado e tente novamente");
- }
- }
- var assinar = await client.AssinarXmlRAsync(empresa.Caminho + "\NFSe-LOTE" + model.IdLote.ToString().PadLeft(15, '0') + ".xml", "Rps", "InfDeclaracaoPrestacaoServico", empresa.Serial, Numero.ToString());
Add Comment
Please, Sign In to add comment