Thinktecture.IdentityModel.Tokens.AccessSecurityTokenHandler.SignToken C# (CSharp) Method

SignToken() protected method

Signs the token.
protected SignToken ( System.Xml.Linq.XElement tokenXml, X509Certificate2 signer ) : System.Xml.Linq.XElement
tokenXml System.Xml.Linq.XElement The token XML.
signer System.Security.Cryptography.X509Certificates.X509Certificate2 The signer.
return System.Xml.Linq.XElement
        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();
        }
    }