コード例 #1
0
ファイル: SigningManager.cs プロジェクト: leandrovip/Vip.DFe
        public static bool ValidarAssinatura <TDocument>(DFeSignDocument <TDocument> document, bool gerarXml) where TDocument : class
        {
            var xml = document.Xml.IsNullOrEmpty() || gerarXml?document.GetXml(SaveOptions.DisableFormatting, Encoding.UTF8) : document.Xml;

            var xmlDoc = new XmlDocument {
                PreserveWhitespace = true
            };

            xmlDoc.LoadXml(xml);
            return(ValidarAssinatura(xmlDoc));
        }
コード例 #2
0
ファイル: SigningManager.cs プロジェクト: leandrovip/Vip.DFe
        /// <summary>
        ///     Gera a assinatura do xml e retorna uma instancia da classe <see cref="DFeSignature" />.
        /// </summary>
        public static DFeSignature AssinarDocumento <TDocument>(this DFeSignDocument <TDocument> document, X509Certificate2 certificado, bool comments, SignDigest digest, SaveOptions options, out string signedXml) where TDocument : class
        {
            Guard.Against <ArgumentException>(!typeof(TDocument).HasAttribute <DFeSignInfoElement>(), "Atributo [DFeSignInfoElement] não encontrado.");

            var xml    = document.GetXml(options, Encoding.UTF8);
            var xmlDoc = new XmlDocument {
                PreserveWhitespace = true
            };

            xmlDoc.LoadXml(xml);

            var signatureInfo = typeof(TDocument).GetAttribute <DFeSignInfoElement>();
            var xmlSignature  = GerarAssinatura(xmlDoc, signatureInfo.SignElement, signatureInfo.SignAtribute, certificado, comments, digest);

            // Adiciona a assinatura no documento e retorna o xml assinado no parametro signedXml
            var element = xmlDoc.ImportNode(xmlSignature, true);

            xmlDoc.DocumentElement?.AppendChild(element);
            signedXml = xmlDoc.AsString(!options.HasFlag(SaveOptions.DisableFormatting), !options.HasFlag(SaveOptions.OmitDeclaration));

            return(DFeSignature.Load(xmlSignature.OuterXml));
        }