Esempio n. 1
0
        public async System.Threading.Tasks.Task TestMethod1Async()
        {
            #region comentado
            //System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\Users\James\desktop\teste assinatura.xml", false);
            //string remessa = sr.ReadToEnd();


            X509Certificate2 xCert = CertificateLoader.FromPath(@"C:\Users\James\Desktop\certificado.pfx", "quesam01");

            //XmlDocument docRequest = new XmlDocument();
            //docRequest.PreserveWhitespace = false;
            //docRequest.LoadXml(sr.ReadToEnd());

            //sr.Close();

            //SignedXml signedXml = new SignedXml();
            //XmlNodeList ListInfNFe = docRequest.GetElementsByTagName("InfRps");

            //foreach (XmlElement infNFe in ListInfNFe)

            //{

            //    string id = infNFe.Attributes.GetNamedItem("Id").InnerText;
            //    signedXml = new SignedXml(infNFe);
            //    signedXml.SigningKey = xCert.PrivateKey;

            //    Reference reference = new Reference("#" + id);
            //    reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
            //    reference.AddTransform(new XmlDsigC14NTransform());
            //    signedXml.AddReference(reference);

            //    KeyInfo keyInfo = new KeyInfo();
            //    keyInfo.AddClause(new KeyInfoX509Data(xCert));

            //    signedXml.KeyInfo = keyInfo;

            //    signedXml.ComputeSignature();

            //    XmlElement xmlSignature = docRequest.CreateElement("Signature", "http://www.w3.org/2000/09/xmldsig#");
            //    XmlElement xmlSignedInfo = signedXml.SignedInfo.GetXml();
            //    XmlElement xmlKeyInfo = signedXml.KeyInfo.GetXml();

            //    XmlElement xmlSignatureValue = docRequest.CreateElement("SignatureValue", xmlSignature.NamespaceURI);
            //    string signBase64 = Convert.ToBase64String(signedXml.Signature.SignatureValue);
            //    XmlText text = docRequest.CreateTextNode(signBase64);
            //    xmlSignatureValue.AppendChild(text);

            //    xmlSignature.AppendChild(docRequest.ImportNode(xmlSignedInfo, true));
            //    xmlSignature.AppendChild(xmlSignatureValue);
            //    xmlSignature.AppendChild(docRequest.ImportNode(xmlKeyInfo, true));

            //    var evento = docRequest.GetElementsByTagName("TAG_EXTERNA_QUE_CONTERA_A_ASSINATURA");
            //    evento[0].AppendChild(xmlSignature);

            //}

            #endregion

            //AssinarXml(@"C:\Users\James\desktop\teste assinatura.xml", "Rps", "InfRps", xCert);

            AssinaturaDigital      assinaturaDigital = new AssinaturaDigital();
            System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\Users\James\desktop\teste assinatura.xml", false);
            string remessa            = sr.ReadToEnd();

            assinaturaDigital.Assinar(remessa, "InfRps", xCert);
            assinaturaDigital.Assinar(assinaturaDigital.XMLStringAssinado, "LoteRps", xCert, "Rps");


            sr.Close();

            StreamWriter sw = new StreamWriter(@"C:\Users\James\desktop\teste assinatura.xml");
            await sw.WriteAsync(assinaturaDigital.XMLStringAssinado.ToCharArray());

            sw.Close();

            // AssinarXml(@"C:\Users\James\desktop\teste assinatura.xml", "EnviarLoteRpsEnvio", "LoteRps", xCert);
        }