public AFirma() { string[] lines = File.ReadAllLines(@"C:\Temp\ConfigAfirma.txt"); _appId = lines[0]; // Linea 1: identificador de la aplicacion _certPath = lines[1]; // Linea 2: ruta donde se encuentra el certificado para firmar las peticiones _password = lines[2]; // Linea 3: password del fichero Identity identity = new Identity(new X509Certificate2(_certPath, _password), _appId); // Se establece el certificado con el cual deben ir firmadas las respuestas del servidor _afirmaService = new AfirmaService(identity, new X509Certificate2(ObtenerRecurso("IntegraAfirmaNet.Test.Certificados.SGAD_SE.cer"))); }
private void btnEnviarSolicitud_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtFicheroFirma.Text)) { MessageBox.Show("Debe seleccionar un fichero de firma"); return; } if (string.IsNullOrEmpty(txtIdentificador.Text)) { MessageBox.Show("Debe introducir el identificador de aplicación de @firma"); return; } /* Selecionar el certificado para firmar la solicitud. El certificado deberá ser el mismo * que el empleado durante el registro en @firma */ X509Certificate2 cert = SeleccionarCertificado(); if (cert == null) { MessageBox.Show("Debe seleccionar un certificado para firmar la solicitud."); return; } try { byte[] contenidoFirma = File.ReadAllBytes(txtFicheroFirma.Text); SignatureFormat formatoFirma; if (Path.GetExtension(txtFicheroFirma.Text).ToUpper() == ".PDF") { if (cmbTipo.SelectedIndex != cmbTipo.Items.Count - 1) { MessageBox.Show("Debe especificar un formato de firma correcto."); } formatoFirma = SignatureFormat.PAdES; } else if (Path.GetExtension(txtFicheroFirma.Text).ToUpper() == ".XML" || Path.GetExtension(txtFicheroFirma.Text).ToUpper() == ".XSIG") { formatoFirma = SignatureFormat.XAdES; } else { formatoFirma = SignatureFormat.CAdES; } Identity identity = new Identity(cert, txtIdentificador.Text); // Certificado que firma las respuestas del servidor AfirmaService afirmaService = new AfirmaService(identity); byte[] resultado = afirmaService.UpgradeSignature(contenidoFirma, formatoFirma, ReturnUpdatedSignatureType.GetReturnUpdatedSignatureType(cmbTipo.Text)); if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { File.WriteAllBytes(saveFileDialog1.FileName, resultado); } MessageBox.Show("Proceso completado correctamente"); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ha ocurrido procesando la solicitud"); } }