本文整理汇总了C#中System.Security.Cryptography.Xml.SignedXml.CheckSignature方法的典型用法代码示例。如果您正苦于以下问题:C# SignedXml.CheckSignature方法的具体用法?C# SignedXml.CheckSignature怎么用?C# SignedXml.CheckSignature使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.Xml.SignedXml
的用法示例。
在下文中一共展示了SignedXml.CheckSignature方法的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: 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.");
}
}
}
示例3: 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;
}
示例4: 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);
}
示例5: 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);
}
示例6: 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");
}
示例7: 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());
}
示例8: 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);
}
示例9: 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();
}
示例10: 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();
}
示例11: IsResponseValid
public bool IsResponseValid(XmlDocument xDoc)
{
XmlNamespaceManager manager = new XmlNamespaceManager(xDoc.NameTable);
manager.AddNamespace("ds", SignedXml.XmlDsigNamespaceUrl);
XmlNodeList nodeList = xDoc.SelectNodes("//ds:Signature", manager);
SignedXml signedXml = new SignedXml(xDoc);
signedXml.LoadXml((XmlElement)nodeList[0]);
X509Certificate2 cSigningCertificate = new X509Certificate2();
cSigningCertificate.Import(HttpContext.Current.Server.MapPath(".") + @"\Certificates\SignCertFromCentrify.cer");
return signedXml.CheckSignature(cSigningCertificate, true);
}
示例12: IsValid
public bool IsValid()
{
bool status = false;
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]);
status = signedXml.CheckSignature(certificateHelper.cert, true);
if (!status)
return false;
return status;
}
示例13: XmlDocumentExtensions_Sign
public void XmlDocumentExtensions_Sign()
{
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<root><content>Some Content</content></root>");
xmlDoc.Sign(TestCert);
var signature = xmlDoc.DocumentElement["Signature", SignedXml.XmlDsigNamespaceUrl];
var signedXml = new SignedXml(xmlDoc);
signedXml.LoadXml(signature);
signedXml.CheckSignature(TestCert, true).Should().BeTrue();
}
示例14: XmlDocumentExtensions_Sign
public void XmlDocumentExtensions_Sign()
{
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<root ID=\"rootElementId\"><content>Some Content</content></root>");
xmlDoc.Sign(TestCert);
var signature = xmlDoc.DocumentElement["Signature", SignedXml.XmlDsigNamespaceUrl];
signature["SignedInfo", SignedXml.XmlDsigNamespaceUrl]
["Reference", SignedXml.XmlDsigNamespaceUrl].Attributes["URI"].Value
.Should().Be("#rootElementId");
var signedXml = new SignedXml(xmlDoc);
signedXml.LoadXml(signature);
signedXml.CheckSignature(TestCert, true).Should().BeTrue();
}
示例15: GeneratedXmlIsCorrectlySigned
public void GeneratedXmlIsCorrectlySigned()
{
var key = new RSACryptoServiceProvider();
var generator = new LicenseGenerator(key);
var rawXml = generator.GenerateSignedXml(new LicenseDetails());
var doc = new XmlDocument();
TextReader reader = new StringReader(rawXml);
doc.Load(reader);
var signedXml = new SignedXml(doc);
var nodeList = doc.GetElementsByTagName("Signature");
signedXml.LoadXml((XmlElement)nodeList[0]);
var result = signedXml.CheckSignature(key);
Assert.IsTrue(result, "Verification of xml signature failed");
}