2009-07-30 20 views
6

üretir. Bir üçüncü taraf hizmetinin uygulanmasına ediyorum ve onlar sadece son zamanlarda tüm mesajları dijital olarak ... imzalanmışSignedXml Ben düzgün davranmaya .NET xmldsig destek almak için daha spesifik SignedXml sınıfını çalışıyorum geçersiz imzalar

Sorunum geçerli imza üretmek için görünmüyor olabilir, yani gerektiğini gerektiren başladık. Hem üçüncü taraf hizmeti hem de bulduğum çevrimiçi imza doğrulayıcısı, imzayı geçersiz olarak bildiriyor. Doğrulama hizmeti (http://www.aleksey.com/xmlsec/xmldsig-verifier.html) özetle veriler arasında bir uyumsuzluk olduğunu bildirir ve şu ana kadar ne yaptığımı anlayamadım.

İlgili kod İşte - umarım birisi benim hata tespit edebilecektir; Herkes ilgileniyor durumunda

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; 
} 

cevap

12

ben de sorunu çözmüş ve bloguma bu konuda şunları yazdı: http://thomasjo.com/blog/2009/08/04/xmldsig-in-the-net-framework.html

+1

Büyük iş! Sanırım yazınız bana çok yardımcı olacak! "Zarflanmış" tipini kullanmam lazım ve var olan belgeler korkunçtu ... –

+1

Makaleniz için çok teşekkürler! –

+1

Doğru URL http://thomasjo.com/blog/2009/08/04/xmldsig-in-the-net-framework.html – Giorgi

İlgili konular