亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 C# 中對沒有 BOM 前導碼的簽名 xml 文件進行 UTF8 編碼

如何在 C# 中對沒有 BOM 前導碼的簽名 xml 文件進行 UTF8 編碼

C#
米脂 2023-07-23 13:58:35
我有一個對我的 xml 文件進行簽名的函數,該文件采用 utf-8 編碼,但是當我應用該符號時,它顯示“utf-8 with Boom”(錯誤版本),我如何正確保存文件private static void FirmarDocumentoComplemento(string pathXmlDocument, string pathCert, string passCert, string pathXmlSignet, string ruta, string rut, string fecha, string tipo){    XmlDocument documentXml = new XmlDocument();    documentXml.Load(pathXmlDocument);    SignedXml firmado = new SignedXml(documentXml);    var cert = RSA_helper.GetX509Certificate(pathCert, passCert);    firmado.SigningKey = (RSA)cert.PrivateKey;    firmado.SignedInfo.SignatureMethod = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";    //digest info    Reference reference = new Reference();    reference.Uri = "";    reference.DigestMethod = "http://www.w3.org/2000/09/xmldsig#sha1";    firmado.AddReference(reference);    // with the public key will be added in the signature part.     KeyInfo keyInfo = new KeyInfo();    keyInfo.AddClause(new RSAKeyValue((RSA)cert.PrivateKey));    keyInfo.AddClause(new KeyInfoX509Data(cert));    firmado.KeyInfo = keyInfo;    firmado.ComputeSignature();    XmlElement xmlDigitalSignature = firmado.GetXml();    // buscamos el ultimo elemento del documento para agregarle la firma    XmlElement elemento = (XmlElement)documentXml.SelectSingleNode(@"//datos_complementarios_declaracion_ingreso/representantes[last()]");    XmlNode parent = elemento.ParentNode;    parent.InsertAfter(xmlDigitalSignature, elemento);    string ruta_completa = ruta + rut + " - " + tipo + " - " + fecha + " - N - " + "Complemento Firmado.xml";    documentXml.Save(ruta_completa);}
查看完整描述

1 回答

?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

XmlWriterSettings?settings?=?new?XmlWriterSettings();
settings.Indent?=?true;
settings.Encoding?=?new?UTF8Encoding(false);string?ruta_completa?=?$"{ruta}{rut}?-?{tipo}?-?{fecha}?-?N?-?Complemento?Firmado.xml";

XmlWriter?writer?=?XmlWriter.Create(ruta_completa,?settings);
doc.Save(writer);

您應該使用XmlTextWriter的實例編寫文檔,并在其構造函數中傳遞一個XmlWriterSettings實例,其中您將Encoding屬性顯式設置為不帶 BOM 前導碼的UTF8Encoding實例



查看完整回答
反對 回復 2023-07-23
  • 1 回答
  • 0 關注
  • 229 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號