internal static bool VerifyXML(string xml) { XmlDocument env = new XmlDocument(); env.PreserveWhitespace = true; env.LoadXml(xml); string soapNs = env.DocumentElement.NamespaceURI; XmlNamespaceManager xmlnm = new XmlNamespaceManager(env.NameTable); xmlnm.AddNamespace("s", soapNs); xmlnm.AddNamespace("def", "http://www.w3.org/2000/09/xmldsig#"); xmlnm.AddNamespace("h", UserNamespace); XmlElement ele = (XmlElement)env.SelectSingleNode("/s:Envelope/s:Header/h:signature/def:Signature", xmlnm); if (ele == null) { return(false); } NehtaSignedXml signedXml = new NehtaSignedXml(env); signedXml.LoadXml(ele); bool answer = signedXml.CheckSignature(); return(answer); }
internal XmlElement Sign(XmlElement element, X509Certificate2 signingCertificate, List <string> references) { XmlDocument xmlDoc = new XmlDocument(); // xmlDoc.LoadXml(element.ToString(SaveOptions.DisableFormatting)); xmlDoc.LoadXml(element.OuterXml); // Create the signature object NehtaSignedXml signedXml = new NehtaSignedXml(xmlDoc); signedXml.SigningKey = signingCertificate.PrivateKey; // Specify the canonicalization method signedXml.Signature.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl; // Specify the signature method signedXml.Signature.SignedInfo.SignatureMethod = SignedXml.XmlDsigRSASHA1Url; // Add all the signing references foreach (string signReferenceId in references) { Reference reference = new Reference(); reference.Uri = "#" + signReferenceId; reference.DigestMethod = SignedXml.XmlDsigSHA1Url; // Add the transform XmlDsigExcC14NTransform transform = new XmlDsigExcC14NTransform(); reference.AddTransform(transform); // Add the reference signedXml.AddReference(reference); } // Calculate the signature signedXml.ComputeSignature(); // Add the key information to the signature signedXml.KeyInfo = new KeyInfo(); signedXml.KeyInfo.AddClause(new KeyInfoX509Data(signingCertificate)); // Return the signature return(signedXml.GetXml()); }