private async Task <EmployerFiscalInformationData> SaveFiscalInformationAsync(EmployerFiscalInformationData data) { //Decrypt cer and key files try { var employerFiscalInformations = new List <EmployerFiscalInformation>(); var digitalSign = new DigitalSign.DigitalSign(); var requiredData = getRequireData(); var clsCrypto = new ClsCrypto(requiredData.Ps, requiredData.IV, requiredData.Salt); data.cfb = clsCrypto.Decrypt(data.cfb).Replace("data:application/x-x509-ca-cert;base64,", String.Empty).Replace("data:application/octet-stream;base64,", String.Empty); data.kfb = clsCrypto.Decrypt(data.kfb).Replace("data:application/x-x509-ca-cert;base64,", String.Empty).Replace("data:application/octet-stream;base64,", String.Empty); if (!String.IsNullOrEmpty(data.cfb) && !String.IsNullOrEmpty(data.kfb)) { var fileCER = Convert.FromBase64String(data.cfb); var fileKEY = Convert.FromBase64String(data.kfb); //validate Cer and Key digitalSign.validateCerKEYContent(fileCER, fileKEY, data.pfb, data.RFC); var clsCryptoToCreate = new ClsCrypto( SessionModel.CompanyID.ToString().ToLower().Replace("-", ""), SessionModel.InstanceID.ToString().ToLower().Replace("-", ""), SessionModel.InstanceID.ToString().ToLower().Replace("-", "").Substring(0, 19)); var certificateCER = clsCryptoToCreate.Encrypt(data.cfb); var certificateKEY = clsCryptoToCreate.Encrypt(data.kfb); var certificatePwd = StringCipher.Encrypt(data.pfb); //Certificate number data.CertificateNumber = digitalSign.GetCertificateNumber(fileCER); //expiration date var resultExpirationDate = digitalSign.GetExpirationDate(fileCER); data.StartDate = resultExpirationDate.Item1.ToShortDateString(); data.ExpirationDate = resultExpirationDate.Item2.ToShortDateString(); employerFiscalInformations.Add(new EmployerFiscalInformation() { ID = data.ID == null ? Guid.NewGuid() : (Guid)data.ID, Active = true, company = SessionModel.CompanyID, Timestamp = DateTime.UtcNow, InstanceID = SessionModel.InstanceID, Description = $"InfoFiscal de {data.RFC}", CreationDate = DateTime.UtcNow, user = SessionModel.IdentityID, Name = "InfoFiscal", StatusID = 1, RFC = data.RFC, CertificateCER = certificateCER, CertificateKEY = certificateKEY, CertificatePwd = certificatePwd, CertificateNumber = data.CertificateNumber, StartDate = resultExpirationDate.Item1, EndDate = resultExpirationDate.Item2 }); if (data.ID != null) { await clientFiscal.UpdateAsync(employerFiscalInformations, SessionModel.CompanyID); } else { await clientFiscal.CreateAsync(employerFiscalInformations, SessionModel.CompanyID); } data.ID = employerFiscalInformations.FirstOrDefault().ID; } else { throw new CotorraException(3012, "3012", "Es necesario especificar el certificado y el key.", null); } } catch (Exception ex) { throw new CotorraException(3011, "3011", $"Ocurrió un error al cargar el certificado: {ex.Message}", ex); } return(data); }
public async Task <JsonResult> Save(EmployerFiscalInformationData data) { data = await SaveFiscalInformationAsync(data); return(Json(data)); }