protected virtual XElement SignToken(XElement tokenXml, X509Certificate2 signer)
{
Contract.Requires(tokenXml != null);
Contract.Requires(signer != null);
Contract.Ensures(Contract.Result<XElement>() != null);
// create SignedXml instance and set signer key
var signedXml = new SignedXml(tokenXml.ToXmlElement());
signedXml.SigningKey = signer.PrivateKey;
// add an enveloped transformation to the reference.
Reference reference = new Reference { Uri = "" };
reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
// add the reference to the SignedXml object.
signedXml.AddReference(reference);
// add a key info to the SignedXml object
KeyInfo keyInfo = new KeyInfo();
keyInfo.AddClause(new KeyInfoName(Convert.ToBase64String(signer.GetCertHash())));
signedXml.KeyInfo = keyInfo;
// compute the signature.
signedXml.ComputeSignature();
// get the XML representation of the signature
return signedXml.GetXml().ToXElement();
}
}