SignedXml.CheckSignature always false
- From: Dieter <Dieter@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 17 Jun 2008 02:37:01 -0700
Hi,
I try to create an XmlSignature (Detached). The Sign Method in my example
works fine. Inside the Verify Method the result of SignedXml.CheckSignature
is always false. Any help or ideas is really appreciated.
How is the strategy to find such problems. Are there any more specific
details inside the debugger to find what is wrong?
Thanks
Dieter
My Example Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
namespace XmlSignatureDetached
{
class Program
{
static void Main(string[] args)
{
Init();
}
private static void Init()
{
string xmlString = "<Assertion><Issuer>Identity
Management</Issuer><Subject><NameID>MyDomain\\MyUser</NameID></Subject><AttributeStatement><Attribute
name=\"IamIdentityState\"><AttributeValue>Activated</AttributeValue></Attribute></AttributeStatement></Assertion>";
XmlDocument xdoc = new XmlDocument();
xdoc.PreserveWhitespace = true;
xdoc.LoadXml(xmlString);
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048);
XmlDocument signature = Sign(xdoc, rsa);
bool verifyResult = Verify(xdoc, signature, rsa);
}
private static XmlDocument Sign(XmlDocument content, RSA rsa)
{
XmlDocument signature = null;
// Create a SignedXml object.
SignedXml signedXml = new SignedXml(content);
// Add the key to the SignedXml document.
signedXml.SigningKey = rsa;
// Create a reference to be signed.
Reference reference = new Reference();
reference.DigestMethod =
"http://www.w3.org/2001/04/xmlenc#sha512";
// Add an enveloped transformation to the reference.
XmlDsigEnvelopedSignatureTransform env = new
XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
reference.Uri = "";
// Add the reference to the SignedXml object.
signedXml.AddReference(reference);
// Compute the signature.
signedXml.ComputeSignature();
// Get the XML representation of the signature
signature = new XmlDocument();
XmlElement xmlDigitalSignature = signedXml.GetXml();
XmlNode xmlNodeDigSig = xmlDigitalSignature.Clone();
signature.AppendChild(signature.ImportNode(xmlNodeDigSig, true));
if (signature.FirstChild is XmlDeclaration)
{
signature.RemoveChild(signature.FirstChild);
}
return signature;
}
private static bool Verify(XmlDocument xdoc,XmlDocument
signature,RSACryptoServiceProvider rsa)
{
bool result = false;
// Create a new SignedXml and pass it to the XmlDocument.
SignedXml signedXml = new SignedXml(xdoc);
// Add the key to the SignedXml document.
signedXml.SigningKey = rsa;
// Find the "Signature" node and create a new XmlNodeList.
XmlNodeList nodeList =
signature.GetElementsByTagName("Signature");
// Load the signature node.
signedXml.LoadXml((XmlElement)nodeList[0]);
// Check the signature and set the result.
result = signedXml.CheckSignature();
return result;
}
}
}
.
- Prev by Date: Attribut AssemblyDescription und Verbatim-Strings
- Next by Date: Re: Win32 auf 2008 Server
- Previous by thread: Attribut AssemblyDescription und Verbatim-Strings
- Next by thread: Re: Win32 auf 2008 Server
- Index(es):
Relevant Pages
|