Esempio n. 1
0
        /// <summary>
        /// Verify the signature of an XML file
        /// </summary>
        /// <param name="fileName">Filename</param>
        /// <returns>True or False</returns>
        public static Boolean VerifyXmlFile(String fileName)
        {
            // Check the args.
            if (null == fileName)
            {
                throw new ArgumentNullException("FileName");
            }

            // Create a new XML document.
            XmlDocument xmlDocument = new XmlDocument();

            xmlDocument.PreserveWhitespace = true;

            // check the file exists
            if (!(System.IO.File.Exists(fileName)))
            {
                LogManager.Log(LOG_FILE.DRCA_BL, "DigitalSignatureManager.VerifyXMLFile(): File not found: " + fileName);
                throw new ArgumentNullException("File not found");
            }

            // Load the passed XML file into the document.
            xmlDocument.Load(fileName);

            // Create a new SignedXml object and pass it
            // the XML document class.
            SignedXml signedXml = new SignedXml(xmlDocument);

            // Find the "Signature" node and create a new
            // XmlNodeList object.
            XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Signature");

            // Load the signature node.
            signedXml.LoadXml((XmlElement)nodeList[0]);

            // Check the signature and return the result.
            bool valid = false;

            try
            {
                valid = signedXml.CheckSignature();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (valid)
            {
                LogManager.Log(LOG_FILE.DRCA_BL, "DigitalSignatureManager.VerifyXMLFile(): " + fileName + " verifed OK");
            }
            else
            {
                LogManager.Log(LOG_FILE.DRCA_BL, "DigitalSignatureManager.VerifyXMLFile(): " + fileName + " failed verify", System.Diagnostics.EventLogEntryType.Error);
                LogManager.AppEventLog("DigitalSignatureManager.VerifyXMLFile(): " + fileName + " failed verify", System.Diagnostics.EventLogEntryType.FailureAudit, 0);
            }
            return(valid);
        }