Ejemplo n.º 1
0
        /// <summary>
        /// Builds key exchange request xml with generated keypair's public key and calls key exchange subcontract to receive providers public key
        /// </summary>S
        /// <param name="fileName"></param>
        /// <param name="subContractId">Key exchange subcontractId given by service provider</param>
        /// <param name="signingCert">Signing certificate</param>
        public void PerformExchange(String fileName, String subContractId, X509Certificate2 signingCert)
        {
            if (GeneratedPublicKey == null)
            {
                GenerateKeyPair();
            }

            var publicKeyXml = GeneratedPublicKey.ToXml();

            var signId = GovTalkHelpers.GenerateSignId();

            var firstChild = (XmlElement)publicKeyXml.FirstChild;

            firstChild.SignXml(signingCert, signId);

            var xmlText = GovTalkHelpers.GetXmlFile(fileName);

            var requestNode = GovTalkHelpers.ComposeRequestXmlWithParamObjects(xmlText, subContractId, publicKeyXml);

            var keyExchangeResponse = GovTalkCallApi.GetResponse(requestNode.OuterXml);

            var keyExchangeDoc = new XmlDocument();

            keyExchangeDoc.LoadXml(keyExchangeResponse);

            //var logFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XmlLogs", $"keyExch_{DateTime.Now:yyyy.MM.dd_hh.mm.ss}.xml");
            //var fileName = HttpContext.Current.Server.MapPath($"~/XmlLogs/keyExch_{DateTime.Now:yyyy.MM.dd_hh.mm.ss}.xml");

            //using (var file = File.CreateText(logFileName))
            //    keyExchangeDoc.Save(file);

            var resultStatus = GovTalkHelpers.GetResponseStatus(keyExchangeDoc);

            if (resultStatus.Code != "14")
            {
                var message = $"KeyExchange error [{resultStatus.Code}] {resultStatus.Message}";
                throw new Exception(message);
            }

            ReceivedPublicKey = new ECPublicKey();
            ReceivedPublicKey.LoadXml(keyExchangeResponse);

            KeyId = ReceivedPublicKey.Id;
        }