سؤال

كنت أحاول الحصول على 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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top