如何使用ExtendedSignedXml类验证XAdES签名?

时间:2017-07-19 15:26:47

标签: c# xml x509certificate xml-signature

我正在尝试验证我使用来自codeplex的XadesNetLib创建的XAdES签名。

我已实现了分离签名,因此签名是指在Reference标记的URI属性中定义的XML文件。但是,签名文件和XML都位于同一文件夹中。我在尝试使用下面的代码验证签名时收到错误“无法解析Uri”

private static VerificationResults PerformValidationFromXml(string xml, VerificationParameters validationParameters)
{
    var document = new XmlDocument { PreserveWhitespace = false };
    document.LoadXml(xml);

    var newsignedXml = new ExtendedSignedXml(document);
    if (document.DocumentElement == null) throw new InvalidDocumentException("Document has no root element");

    var signatureNode = XmlDsigNodesHelper.GetSignatureNode(document);
    newsignedXml.LoadXml(signatureNode);

    var verificationCertificate = GetVerificationCertificate(newsignedXml, validationParameters);
    if (verificationCertificate == null) throw new Exception("Signer public key could not be found");
    if (!newsignedXml.CheckSignature(verificationCertificate, !validationParameters.VerifyCertificate))
    {
        throw new InvalidOperationException("Signature is invalid.");
    }
    var results = new VerificationResults
    {
        Timestamp = GetTimeStampFromSignature(document),
        OriginalDocument = GetDocumentFromSignature(document),
        SigningCertificate = GetCertificateFromSignature(document)
    };

    return results;

}

非常感谢任何帮助!

0 个答案:

没有答案