5 using System.Collections.Generic;
9 using System.Security.Cryptography.X509Certificates;
20 #region Propiedades privadas 21 List<string> _lstFileNameCert =
new List<string>();
22 private string _cMensajeError =
string.Empty;
23 #endregion _cMensajeError 32 #endregion Constructor 34 #region Propiedades públicas 35 public string _MensajeError
40 get {
return _cMensajeError; }
42 #endregion Propiedades públicas 54 #region Métodos públicos 55 public string _GetFileCertificate(
string tcCertificateName,
string tcCertificatePassword =
"")
63 X509Certificate2 loCertX509 = null;
64 string lcFileNameCert =
string.Empty;
68 byte[] fileContent = null;
70 if (tcCertificateName ==
"ENTIDAD_FACEB2B")
72 fileContent = RawCertificate(tcCertificateName);
76 loCertX509 = GetCertificate(tcCertificateName, tcCertificatePassword);
78 if (loCertX509 != null)
79 fileContent = loCertX509.Export(X509ContentType.Pfx, tcCertificatePassword);
82 if (fileContent.Length > 0)
84 lcFileNameCert = Path.GetTempFileName();
85 File.WriteAllBytes(lcFileNameCert, fileContent);
86 _lstFileNameCert.Add(lcFileNameCert);
90 lcFileNameCert =
string.Empty;
91 _cMensajeError =
string.Format(
"El certificado '{0}' no se ha importado correctamente. ", tcCertificateName);
94 catch (SecurityException ex)
96 _cMensajeError =
string.Format(
"El certificado '{0}' no se ha importado correctamente. {1}", tcCertificateName, ex.Message);
100 _cMensajeError =
string.Format(
"El certificado '{0}' no se ha importado correctamente. {1}", tcCertificateName, ex);
103 return lcFileNameCert;
113 #endregion Métodos públicos 115 #region Métodos privados 116 private void CloseService()
121 if (_lstFileNameCert.Count > 0)
123 foreach (
string lcFile
in _lstFileNameCert)
126 _lstFileNameCert.Clear();
134 private void DeleteFile(
string tcFileName)
138 if (File.Exists(tcFileName))
139 File.Delete(tcFileName);
151 private Byte[] RawCertificate(
string tcCertificateName)
153 Byte[] loRawCertif = null;
155 switch (tcCertificateName)
157 case "SAGESPAIN_SHA1":
158 if (FUNCTIONS._EntornoFaceB2B() ==
eEntornos.Pre)
159 loRawCertif = Properties.Resources.SAGESPAIN_PF_PRUEBAS;
161 loRawCertif = Properties.Resources.SAGESPAIN_SHA1;
165 if (FUNCTIONS._EntornoFaceB2B() ==
eEntornos.Pre)
166 loRawCertif = Properties.Resources.SAGESPAIN_PF_PRUEBAS;
168 loRawCertif = Properties.Resources.SAGESPAIN;
171 case "ENTIDAD_FACEB2B":
172 if (FUNCTIONS._EntornoFaceB2B() ==
eEntornos.Pre)
173 loRawCertif = Properties.Resources.SELLO_ENTIDAD_SGAD_PRUEBAS;
175 loRawCertif = Properties.Resources.SELLO_DE_ENTIDAD_SGAD;
191 private X509Certificate2 GetCertificate(
string tcCertificateName,
string tcCertificatePassword)
193 if (
String.IsNullOrEmpty(tcCertificateName))
194 throw new SecurityException(
"Nombre del certificado no informado.");
196 string lcError =
string.Empty;
198 X509Certificate2 loCertX509 = null;
202 if (tcCertificateName ==
"SAGESPAIN")
204 loCertX509 = GetCertificateFromResources(tcCertificateName, tcCertificatePassword, ref lcError);
206 if (loCertX509 == null)
207 loCertX509 = GetCertificateFromResources(
"SAGESPAIN_SHA1", tcCertificatePassword, ref lcError);
211 Byte[] loRawCertif = RawCertificate(tcCertificateName);
213 if (loRawCertif.Length > 0)
215 loCertX509 =
new X509Certificate2();
216 loCertX509.Import(loRawCertif, tcCertificatePassword, X509KeyStorageFlags.Exportable);
222 _cMensajeError =
string.Format(
"El certificado '{0}' no se ha importado correctamente. {1}", tcCertificateName, ex.Message);
225 if (loCertX509 == null)
227 if (
string.IsNullOrWhiteSpace(lcError))
228 _cMensajeError =
string.Format(
"El certificado '{0}' indicado no se encuentra o está caducado.", tcCertificateName);
230 _cMensajeError = lcError;
243 private X509Certificate2 GetCertificateFromResources(
string tcCertificateName,
string tcCertificatePassword, ref
string tcError)
245 X509Certificate2 certX509 = null;
250 Byte[] raw = RawCertificate(tcCertificateName);
254 certX509 =
new X509Certificate2();
255 certX509.Import(raw, tcCertificatePassword, X509KeyStorageFlags.Exportable);
260 tcError =
string.Format(
"El certificado '{0}' no se ha importado correctamente. {1}", tcCertificateName, ex);
266 #endregion Métodos privados
Interfaz para obtener certificados
void _CloseService()
Close Certificate Service
eEntornos
Entornos de trabajo