Guest User

Untitled

a guest
Jan 17th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. public Resultado AssinarXml(string arquivo, string tagAssinatura, string tagAtributoId, string serial, string numeroRPS)
  2. {
  3. try
  4. {
  5. SR = System.IO.File.OpenText(arquivo);
  6. string xmlString = SR.ReadToEnd();
  7. SR.Close();
  8. SR = null;
  9.  
  10. XmlDocument doc = new XmlDocument();
  11.  
  12. doc.PreserveWhitespace = false;
  13.  
  14. doc.LoadXml(xmlString);
  15.  
  16. if (doc.GetElementsByTagName(tagAssinatura).Count == 0)
  17. {
  18. resultado.Result = false;
  19. resultado.Mensagem = $"A tag de assinatura {tagAssinatura.Trim()} não existe no XML. (Código do Erro: 5)";
  20.  
  21. Console.WriteLine(resultado.Mensagem);
  22.  
  23. return resultado;
  24. //throw new Exception($"A tag de assinatura {tagAssinatura.Trim()} não existe no XML. (Código do Erro: 5)");
  25. }
  26. else if (doc.GetElementsByTagName(tagAtributoId).Count == 0)
  27. {
  28. resultado.Result = false;
  29. resultado.Mensagem = $"A tag de assinatura {tagAtributoId.Trim()} não existe no XML. (Código do Erro: 4)";
  30.  
  31. Console.WriteLine(resultado.Mensagem);
  32.  
  33. return resultado;
  34. //throw new Exception($"A tag de assinatura {tagAtributoId.Trim()} não existe no XML. (Código do Erro: 4)");
  35. }
  36. else
  37. {
  38. XmlDocument XMLDoc;
  39.  
  40. XmlNodeList lists = doc.GetElementsByTagName(tagAssinatura);
  41. foreach (XmlNode nodes in lists)
  42. {
  43. foreach (XmlNode childNodes in nodes.ChildNodes)
  44. {
  45. if (!childNodes.Name.Equals(tagAtributoId))
  46. continue;
  47.  
  48. if (childNodes.NextSibling != null && childNodes.NextSibling.Name.Equals("Signature"))
  49. continue;
  50.  
  51. Reference reference = new Reference("");
  52. reference.Uri = "";
  53.  
  54. XmlElement childElemen = (XmlElement)childNodes;
  55. if (childElemen.GetAttributeNode("Id") != null)
  56. {
  57. reference.Uri = "#" + "RPS" + numeroRPS.ToString().PadLeft(15, '0');
  58. }
  59. else if (childElemen.GetAttributeNode("id") != null)
  60. {
  61. reference.Uri = "#" + childElemen.GetAttributeNode("id").Value;
  62. }
  63.  
  64. SignedXml signedXml = new SignedXml(doc);
  65.  
  66.  
  67. KeyInfo keyInfo = new KeyInfo();
  68.  
  69. signedXml.KeyInfo = keyInfo;
  70. signedXml.SigningKey = x509Cert.PrivateKey;
  71. signedXml.SignedInfo.SignatureMethod = SignedXml.XmlDsigRSASHA1Url;
  72. reference.DigestMethod = SignedXml.XmlDsigSHA1Url;
  73.  
  74.  
  75. XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
  76. reference.AddTransform(env);
  77. XmlDsigC14NTransform c14 = new XmlDsigC14NTransform();
  78. reference.AddTransform(c14);
  79.  
  80. signedXml.AddReference(reference);
  81.  
  82. keyInfo.AddClause(new KeyInfoX509Data(x509Cert));
  83.  
  84. signedXml.KeyInfo = keyInfo;
  85. signedXml.ComputeSignature();
  86.  
  87. XmlElement xmlDigitalSignature = signedXml.GetXml();
  88.  
  89. nodes.AppendChild(doc.ImportNode(xmlDigitalSignature, true));
  90.  
  91. }
  92. }
  93.  
  94. XMLDoc = new XmlDocument();
  95. XMLDoc.PreserveWhitespace = false;
  96. XMLDoc = doc;
  97. string conteudoXMLAssinado = XMLDoc.OuterXml;
  98. using (StreamWriter sw = System.IO.File.CreateText(arquivo))
  99. {
  100. sw.Write(conteudoXMLAssinado);
  101. sw.Flush();
  102. sw.Close();
  103. }
  104.  
  105. resultado.Result = true;
  106. resultado.Mensagem = $"Procecesso de assinatura efetuado com sucesso!!!";
  107.  
  108. Console.WriteLine(resultado.Mensagem);
  109.  
  110. return resultado;
  111. }
  112. }
  113. catch (Exception ex)
  114. {
  115. //cod = "00000"; msgm = "Certificado não identificado, reinicie o certificado e tente novamente";
  116.  
  117. resultado.Result = false;
  118. resultado.Mensagem = "Código: 00000 - Certificado não identificado, reinicie o certificado e tente novamente";
  119.  
  120. Console.WriteLine(ex);
  121.  
  122. return resultado;
  123. //throw new Exception("Código: 00000 - Certificado não identificado, reinicie o certificado e tente novamente");
  124. }
  125. }
  126.  
  127. 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