예제 #1
0
        private bool  ValidaCertificado()
        {
            string strCertFilePath;
            string strKeyFilePath;

            if (ViewState["idemisor"] == null)
            {
                //var emisor = FContext.TblEmisores.FirstOrDefault(Emisor => Emisor.PKEmisorId == _idemisor);

                Utilities.creaCarpetaClientes(this.txtRFC.Text.ToUpper(), HttpContext.Current.Server.MapPath(""));

                if (fuCertificado.HasFile && fuLlave.HasFile && !string.IsNullOrEmpty(this.txtPassword.Text))
                {
                    //obtenemos la ruta donde se ha guardado
                    strCertFilePath = Utilities.GetServerFolder(this.txtRFC.Text.ToUpper()) + "certificado.cer";
                    strKeyFilePath  = Utilities.GetServerFolder(this.txtRFC.Text.ToUpper()) + "llave.key";

                    //eliminamos la ruta anterior
                    if (System.IO.File.Exists(strCertFilePath))
                    {
                        System.IO.File.Delete(strCertFilePath);
                    }

                    //
                    if (System.IO.File.Exists(strKeyFilePath))
                    {
                        System.IO.File.Delete(strKeyFilePath);
                    }

                    //guardamos la llave y el certificado
                    fuCertificado.SaveAs(strCertFilePath);
                    fuLlave.SaveAs(strKeyFilePath);

                    _Certificado = new Certificado(strKeyFilePath);

                    if (_Certificado.CargarLlavePrivada(strKeyFilePath, txtPassword.Text) == false)
                    {
                        lblError.Text = "Verifique que la contraseña corresponda a la llave privada";
                        return(false);
                    }


                    //Validamos si el certificado es vigente
                    if (!_Certificado.EsVigente)
                    {
                        lblError.Text = "La fecha del Certificado no es vigente.";
                        return(false);
                    }

                    //Validamos que corresponda la llave al certificado
                    if (!_Certificado.CorrespondeLlave)
                    {
                        lblError.Text = "El archivo de llave privada no corresponde al certificado. Favor de verificar los archivos.";
                        return(false);
                    }


                    //Valida que el certificado pertenezca al campo RFC
                    if (txtPassword.Text != "Test123$")
                    {
                        //Se utiliza esta contraseña para poder bincar esta validación
                        String strSubject = _Certificado.certificado.Subject;

                        if (!strSubject.Contains(txtRFC.Text.Trim().ToUpper()))
                        {
                            lblError.Text = "El RFC no coincide con el del certificado. Favor de verificar la información.";
                            return(false);
                        }
                    }

                    try
                    {
                        System.IO.File.Delete(strCertFilePath);
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
            }
            return(true);
        }