本文整理汇总了C#中System.Security.Cryptography.Xml.SignedXml.LoadXml方法的典型用法代码示例。如果您正苦于以下问题:C# SignedXml.LoadXml方法的具体用法?C# SignedXml.LoadXml怎么用?C# SignedXml.LoadXml使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.Xml.SignedXml
的用法示例。
在下文中一共展示了SignedXml.LoadXml方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ValidateLicenseXml
public LicenseDetails ValidateLicenseXml(string xml)
{
var doc = new XmlDocument();
using (TextReader reader = new StringReader(xml))
{
try
{
doc.Load(reader);
}
catch
{
throw new InvalidLicenseXmlException();
}
// Validate the xml's signature
var signedXml = new SignedXml(doc);
var nodeList = doc.GetElementsByTagName("Signature");
if (nodeList.Count == 0)
throw new LicenseSignatureMissingException();
signedXml.LoadXml((XmlElement) nodeList[0]);
if (!signedXml.CheckSignature(_key))
throw new LicenseSignatureMismatchException();
}
// Deserialize the xml
var deserializer = new XmlSerializer(typeof(LicenseDetails));
using (TextReader reader = new StringReader(xml))
return (LicenseDetails) deserializer.Deserialize(reader);
}
示例2: ExtractSignature
/// <summary>
/// Gets the signature from an XmlDocument.
/// </summary>
/// <param name="xmlDocument">The source XmlDocument.</param>
/// <returns>A SignedXml object representing the signature.</returns>
private static SignedXml ExtractSignature(XmlDocument xmlDocument)
{
var signedXml = new SignedXml(xmlDocument);
XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Signature");
signedXml.LoadXml((XmlElement)nodeList[0]);
return signedXml;
}
示例3: VerifyXml
// Verify the signature of an XML file against an asymmetric
// algorithm and return the result.
public static Boolean VerifyXml(XmlDocument Doc, RSA Key)
{
// Check arguments.
if (Doc == null)
throw new ArgumentException("Doc");
if (Key == null)
throw new ArgumentException("Key");
// Create a new SignedXml object and pass it
// the XML document class.
SignedXml signedXml = new SignedXml(Doc);
// Find the "Signature" node and create a new
// XmlNodeList object.
XmlNodeList nodeList = Doc.GetElementsByTagName("Signature");
// Throw an exception if no signature was found.
if (nodeList.Count <= 0)
{
throw new CryptographicException("Verification failed: No Signature was found in the document.");
}
//One Sig per document
if (nodeList.Count >= 2)
{
throw new CryptographicException("Verification failed: More that one signature was found for the document.");
}
// Load the first <signature> node.
signedXml.LoadXml((XmlElement)nodeList[0]);
// Check the signature and return the result.
return signedXml.CheckSignature(Key);
}
示例4: LoadXmlMalformed1
public void LoadXmlMalformed1 ()
{
SignedXml s = new SignedXml ();
XmlDocument doc = new XmlDocument ();
doc.LoadXml ("<root/>");
s.LoadXml (doc.DocumentElement);
}
示例5: IsSignedByAny
/// <summary>
/// Checks if an xml element is signed by the given certificate, through
/// a contained enveloped signature.
/// </summary>
/// <param name="xmlElement">Xml Element that should be signed</param>
/// <param name="signingKeys">Signing keys to test, one should validate.</param>
/// <param name="validateCertificate">Should the certificate be validated too?</param>
/// <returns>True on correct signature, false on missing signature</returns>
/// <exception cref="InvalidSignatureException">If the data has
/// been tampered with or is not valid according to the SAML spec.</exception>
public static bool IsSignedByAny(
this XmlElement xmlElement,
IEnumerable<SecurityKeyIdentifierClause> signingKeys,
bool validateCertificate)
{
if (xmlElement == null)
{
throw new ArgumentNullException(nameof(xmlElement));
}
var signedXml = new SignedXml(xmlElement);
var signatureElement = xmlElement["Signature", SignedXml.XmlDsigNamespaceUrl];
if (signatureElement == null)
{
return false;
}
signedXml.LoadXml(signatureElement);
ValidateSignedInfo(signedXml, xmlElement);
VerifySignature(signingKeys, signedXml, signatureElement, validateCertificate);
return true;
}
示例6: LoadXmlMalformed2
public void LoadXmlMalformed2 ()
{
SignedXml s = new SignedXml ();
XmlDocument doc = new XmlDocument ();
doc.LoadXml ("<ds:Signature xmlns:ds='http://www.w3.org/2000/09/xmldsig#'><foo/><bar/></ds:Signature>");
s.LoadXml (doc.DocumentElement);
}
示例7: VerifyXml
public void VerifyXml(string xml)
{
var doc = LoadXmlDoc(xml);
using (var rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(publicKey);
var nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("sig", "http://www.w3.org/2000/09/xmldsig#");
var signedXml = new SignedXml(doc);
var signature = (XmlElement)doc.SelectSingleNode("//sig:Signature", nsMgr);
if (signature == null)
{
throw new Exception("Xml is invalid as it has no XML signature");
}
signedXml.LoadXml(signature);
if (!signedXml.CheckSignature(rsa))
{
throw new Exception("Xml is invalid as it failed signature check.");
}
}
}
示例8: VerifyDigitalSignature
/// <summary>
/// Verifies the digital signature.
/// </summary>
/// <param name="digitalSignature"> The XML Digital Signature.</param>
/// <param name="publicKey"> The RSA public key.</param>
/// <returns> Returns true if valid, else false.</returns>
public static bool VerifyDigitalSignature(XmlTextReader digitalSignature, RSA publicKey)
{
bool valid = false;
try
{
// Load license file into XmlDocument
XmlDocument doc = new XmlDocument();
doc.Load(digitalSignature);
// Load Signature Element
SignedXml verifier = new SignedXml(doc);
verifier.LoadXml(doc.GetElementsByTagName("Signature")[0] as XmlElement);
// Validate license.
if ( verifier.CheckSignature(publicKey) )
{
valid = true;
}
else
{
valid = false;
}
}
catch
{
valid = false;
}
return valid;
}
示例9: CertificateChain
/// <summary>
/// Creates a chain of X509Certificates given the provided XML-DSig.
/// </summary>
/// <param name="xmlDoc">XML-Dsig used to create the chain.</param>
/// <returns>Chain of X509Certificates</returns>
public static List<X509Certificate2> CertificateChain(string xmlDoc)
{
if (xmlDoc == null)
{
throw new ArgumentException("xmlDoc was null");
}
var xml = XmlUtil.LoadXml(xmlDoc);
var xmlNamespaces = new XmlNamespaceManager(xml.NameTable);
xmlNamespaces.AddNamespace("ds", SignedXml.XmlDsigNamespaceUrl);
var sigElement = (XmlElement)xml.SelectSingleNode("//ds:Signature[1]", xmlNamespaces);
var signature = new SignedXml(xml);
signature.LoadXml(sigElement);
var certificates = new List<X509Certificate2>();
foreach (var clause in signature.KeyInfo)
{
if (!(clause is KeyInfoX509Data)) continue;
foreach (var x509Cert in ((KeyInfoX509Data)clause).Certificates)
{
certificates.Add((X509Certificate2)x509Cert);
}
}
return certificates;
}
示例10: IsValid
public bool IsValid() {
XmlNamespaceManager manager = new XmlNamespaceManager(XmlDoc.NameTable);
manager.AddNamespace("ds", SignedXml.XmlDsigNamespaceUrl);
XmlNodeList nodeList = XmlDoc.SelectNodes("//ds:Signature", manager);
SignedXml signedXml = new SignedXml(XmlDoc);
signedXml.LoadXml((XmlElement)nodeList[0]);
return signedXml.CheckSignature(DecrypingCertificate, true);
}
示例11: Constructor_XmlDocument
public void Constructor_XmlDocument ()
{
XmlDocument doc = new XmlDocument ();
doc.LoadXml (signature);
XmlNodeList xnl = doc.GetElementsByTagName ("Signature", SignedXml.XmlDsigNamespaceUrl);
XmlElement xel = (XmlElement) xnl [0];
SignedXml sx = new SignedXml (doc);
sx.LoadXml (doc.DocumentElement);
Assert.IsTrue (sx.CheckSignature (), "CheckSignature");
}
示例12: CheckSignedXmlDocument
public static Boolean CheckSignedXmlDocument(Stream sourceXmlFile)
{
// Carico il documento XML
XmlDocument doc = new XmlDocument();
doc.Load(sourceXmlFile);
// Verifico la firma
SignedXml sigs = new SignedXml(doc);
XmlNodeList sigElems = doc.GetElementsByTagName("Signature");
sigs.LoadXml((XmlElement)sigElems[0]);
return (sigs.CheckSignature());
}
示例13: XmlIsValid
public static bool XmlIsValid(XmlDocument signedXml,
AsymmetricAlgorithm key)
{
var nsm = new XmlNamespaceManager(new NameTable());
nsm.AddNamespace("dsig", SignedXml.XmlDsigNamespaceUrl);
var signatureGenerator = new SignedXml(signedXml);
var signatureNode = signedXml
.SelectSingleNode("//dsig:Signature", nsm);
signatureGenerator.LoadXml((XmlElement)signatureNode);
return signatureGenerator.CheckSignature(key);
}
示例14: IsValid
public bool IsValid(KeyInfo keyInfo)
{
SignedXml xml = new SignedXml(_doc);
XmlNodeList nodeList = _doc.GetElementsByTagName("Signature");
xml.LoadXml((XmlElement)nodeList[0]);
xml.KeyInfo = keyInfo;
xml.Resolver = null;
return xml.CheckSignature();
}
示例15: Main
static void Main(string[] args)
{
var xml = "<xml><a ID=\"foo\"><content>foo-content</content><Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\" /></a><a ID=\"bar\"><content>bar-content</content><Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\" /></a></xml>";
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
var key = new RSACryptoServiceProvider();
var sign = new SignedXml(xmlDocument);
var reference2 = new Reference("#bar");
reference2.AddTransform(new XmlDsigEnvelopedSignatureTransform());
sign.AddReference(reference2);
sign.SigningKey = key;
sign.ComputeSignature();
var barNode = (XmlElement)xmlDocument.SelectSingleNode("//*[@ID=\"bar\"]");
barNode.AppendChild(xmlDocument.ImportNode(sign.GetXml(), true));
var barSignature = barNode.ChildNodes.OfType<XmlElement>()
.Single(x => x.LocalName == "Signature" && x.HasChildNodes);
WriteLine("== Xml document ==");
WriteLine(xmlDocument.OuterXml);
WriteLine();
var verify = new SignedXml(xmlDocument);
verify.LoadXml(barSignature);
WriteLine("Check Signature: " + verify.CheckSignature(key));
WriteLine();
WriteLine("Reloading SignedXml and fixing signature index...");
verify.LoadXml(barSignature);
FixSignatureIndex(verify, barSignature);
WriteLine("Check Signature: " + verify.CheckSignature(key));
ReadLine();
}