c# – SignedXml生成无效签名
发布时间:2020-12-15 04:24:06 所属栏目:百科 来源:网络整理
导读:我一直在努力使.NET中的 XMLDSIG支持正常运行,更具体地说是SignedXml类.我正在实施第三方服务,他们最近才开始要求所有消息都必须经过数字签名…… 我的问题是,我似乎无法生成有效的签名.第三方服务和我找到的在线签名验证器都将签名报告为无效.验证服务(http
我一直在努力使.NET中的
XMLDSIG支持正常运行,更具体地说是SignedXml类.我正在实施第三方服务,他们最近才开始要求所有消息都必须经过数字签名……
我的问题是,我似乎无法生成有效的签名.第三方服务和我找到的在线签名验证器都将签名报告为无效.验证服务(http://www.aleksey.com/xmlsec/xmldsig-verifier.html)报告摘要和数据之间存在不匹配,到目前为止我还无法弄清楚我做错了什么. 这是相关的代码 – 希望有人能够发现我的错误; public static XDocument SignDocument(XDocument originalDocument,X509Certificate2 certificate) { var document = new XmlDocument(); document.LoadXml(originalDocument.ToString(SaveOptions.DisableFormatting)); if (document.DocumentElement == null) throw new InvalidOperationException("Invalid XML document; no root element found."); var signedDocument = new SignedXml(document); Reference signatureReference = GetSignatureReference(); KeyInfo certificateKeyInfo = GetCertificateKeyInfo(certificate); var dataObject = new DataObject("","text/xml","utf-8",document.DocumentElement); signedDocument.AddReference(signatureReference); signedDocument.AddObject(dataObject); signedDocument.SigningKey = certificate.PrivateKey; signedDocument.KeyInfo = certificateKeyInfo; signedDocument.ComputeSignature(); return XDocument.Parse(signedDocument.GetXml().OuterXml,LoadOptions.PreserveWhitespace); } private static Reference GetSignatureReference() { var signatureReference = new Reference(""); signatureReference.AddTransform(new XmlDsigEnvelopedSignatureTransform()); return signatureReference; } private static KeyInfo GetCertificateKeyInfo(X509Certificate certificate) { var certificateKeyInfo = new KeyInfo(); certificateKeyInfo.AddClause(new KeyInfoX509Data(certificate)); return certificateKeyInfo; } 解决方法
如果有人有兴趣,我解决了这个问题并在我的博客上写了这篇文章:
http://thomasjo.com/blog/2009/08/04/xmldsig-in-the-net-framework.html (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- postgresql使用pg_dump和pg_restore 实现跨服务器的数据库迁
- 异常The file cannot be validated as the XML definition
- node.js+express制作网页计算器
- win7下oracle 10g安装图文教程
- MSBuild – 如何从预先写入的命令行命令构建.NET解决方案文
- c – GCC在lambda中对我的静态变量做什么?
- ruby-on-rails – Ruby Enterprise Edition与Matz’Ruby
- swift开发之Selector
- 正则表达式 – 使用Perl自动化形态标记
- 基于C#泛型的对象对象映射器问题