0

I want to use RestSharp and not Microsoft to communicate with that API. I am not very familiar with RestSharp, so I am asking someone with experience to look at my code an the BtnGetCertificate and advise me on how should I change my code to make it work. At the moment, I have no error on the form or code, but when I click on the BtnGetCertificate button, absolutely nothing happens.

FrmCertificateRequest image:`

using System;
using Newtonsoft.Json;
using RestSharp;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using FO_Framework;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;
using System.Security.Policy;
using System.Web.UI.WebControls;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Tab;

namespace SEV_AutoCaisse.FormsGUI
{
    public partial class FrmCertificateRequest : Form
    {
        public FrmCertificateRequest()
        {
            InitializeComponent();
            GetCertificates();
            DeleteCertificate();
        }

        private void BtnGetCertificate_Click(object sender, EventArgs e)
        {
            try
            {
                List<KeyValuePair<String, String>> HTTPHeadersDict = GetHTTPHeaders();
                List<KeyValuePair<String, String>> HTTPHeadersDicti = new List<KeyValuePair<String, String>>();
                var client = new RestClient("https://cnfr.api.rq-fo.ca/enrolement");
                client.Timeout = -1;
                var request = new RestRequest(Method.POST);
                client.AddDefaultHeader("Accept", "application/json");
                client.AddDefaultHeader("User-Agent", "WinFormsApp");
                request.AddHeader("POST", "HTTP/2.0");
                request.AddHeader("Content-Length", "1000");
                request.AddHeader("Content-Type", "application/json");
                request.AddHeader("HOST", "");
                request.AddHeader("ENVIRN", "DEV");
                request.AddHeader("CASESSAI", "000.000");
                request.AddHeader("APPRLINIT", "SEV");
                request.AddHeader("IDAPPRL", "0000-0000-0000");
                request.AddHeader("IDSEV", "000000000000310A");
                request.AddHeader("IDVERSI", "00000000000039EE");
                request.AddHeader("CODCERTIF", "FOB201999999");
                request.AddHeader("IDPARTN", "0000000000000886");
                request.AddHeader("VERSI", "AC.10001");
                request.AddHeader("VERSIPARN", "0");
                request.AddJsonBody(new
                {
                    modif = "AJO",
                    csr = @"-----BEGIN CERTIFICATE REQUEST-----\n IIBwjCCAWegAwIBAgIQSG1zSj0y3qdHxJcGpxYpODAKBggqhkjOPQQDAjBJMQswCQYDVQQGEwJDQTEVMBMGA1UECwwMMEMyMTIzNDUwMDFBMR
MwEQYDVQQKDAoxMjM0NTY3OdkwMQ4wDAYDVQQDDAUxMjNBQjAeFw0xODA1MjQxMTQ0MTJaFw0zMzA1MjUxMTQ0MTJaMEkxCzAJBgNVBAYTAkN
BMRUwEwYDVQQLDAwwQzIxMjM0NTAwMUExEzARBgNVBAoMCjEyMzQ1Njc4OTAxDjAMBgNVBAMMBTEyM0FCMFkwEwYHKoZIzj0CAQYIKoZIzj0D
AQcDQgAElfaEjRXC4hbHK+HZHG05O8V99QLTPGHlhML+sCHbYusa8p4yWFHRmSoY5nHbDaY1ZavBHpGW11ssWvk/jYZ9NKMxMC8wDgYDVR0PA
QH/BAQDAgbAMB0GA1UdDgQWBBSz+EtxlbOyXeG5bU9q+5fBRMMJvzAKBggqhkjOPQQDAgNJADBGAiEA7aRl2CRw/b1Lgej++wcha+8i9NZZXf
nqpDstjrswj9wCIQCCyTdKNHuk6PtloeIZwydV0JnrsMWmjZYkLFzjc+0pyQ==\n -----END CERTIFICATE REQUEST-----"
                }
                );
                IRestResponse response = client.Execute(request);
                Console.WriteLine(response.Content);
                txtResponse.Text = response.Content;

            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex);
            }
        }

        #region Certificates
        /// <summary>
        /// Show the steps required to obtain the SRS and WEB-SRM certificates
        /// Montre les étapes à faire pour obtenir les certificats du SEV et du MEV
        /// </summary>
        private void GetCertificates()
        {
            try
            {
                // KeyPair Creation (public and private)
                // Création de la paire de clés (publique et privée) 
                UtilesECDSA.KeyPairCreation(_KeyPairName, _APPRLINIT, true);

                // Creation of the certificate and of the CSR
                // Création du certificat et demande de signature au MEV-WEB
                String CSR = CertificateCreation();

                // Preparation of the json document to send to the WEB-SRM
                // Préparation du document json pour transmission au MEV-WEB
                String CSRJSon = UtilesJSON.GetJsonCertificate(ValeursPossibles.Modif.AJO, CSR, null);

                // HTTP Headers
                // Entêtes HTTP
                List<KeyValuePair<String, String>> HTTPHeadersDict = GetHTTPHeaders();

                // Send the CSR to the WEB-SRM and reception of the certificates
                // Envoi du CSR au MEV-WEB et réception des certificats
                WEBSRM_Response WEB_SRM_Response = UtilesJSON.CertificatesRequest(HTTPHeadersDict, CSRJSon);

                // HTTP Status codes and description
                if (_Verbose)
                {
                    Console.WriteLine("HTTP Status Code : " + WEB_SRM_Response.StatusCode);
                    Console.WriteLine("HTTP Status Description : " + WEB_SRM_Response.StatusDescription);
                }

                // Processing of error messages received from WEB-SRM, if any
                // Traitement des messages d'erreurs reçus du MEV-WEB, s'il y a lieu
                if (WEB_SRM_Response.ResponseJsonWEBSRM.Contains("listErr"))
                {
                    List<ErrorWEBSRM> lstErr = UtilesJSON.ParseErrors(WEB_SRM_Response.ResponseJsonWEBSRM);
                    if (lstErr.Count > 0)
                        foreach (ErrorWEBSRM er in lstErr)
                        {
                            Console.WriteLine("Id : " + er.Id);
                            Console.WriteLine("Message : " + er.Mess);

                            if (er.CodRetour != "")
                                Console.WriteLine("Code de retour : " + er.CodRetour);
                        }
                }
                else
                {
                    if (!WEB_SRM_Response.ResponseJsonWEBSRM.Contains("Error") && !WEB_SRM_Response.ResponseJsonWEBSRM.Contains("délai d'attente") && !WEB_SRM_Response.ResponseJsonWEBSRM.Contains("La connexion sous-jacente a été fermée"))
                    {
                        // Extraction of information received from WEB-SRM
                        // Extraction des informations reçues du MEV-WEB
                        ResponseCertificats repCertificat = UtilesJSON.ParseResponseCertificates(WEB_SRM_Response.ResponseJsonWEBSRM);

                        // Device identifier
                        // ID Appareil reçu du MEV-WEB
                        _IDAPPRL = repCertificat.IdApprl;

                        // SRS's Certificate
                        // Certificat du SEV
                        X509Certificate2 certSEV = new X509Certificate2(Convert.FromBase64String(repCertificat.Certificat.Replace("-----BEGIN CERTIFICATE-----", "")
                            .Replace("-----END CERTIFICATE-----", "").Replace(Environment.NewLine, "")), "", X509KeyStorageFlags.PersistKeySet)
                        {
                            FriendlyName = _SRSCertificatUserFriendlyName + "-" + _KeyPairName
                        };

                        // Get the private key created above
                        // Récupération de la clef privée créée plus haut
                        CngKey clefs = CngKey.Open(_KeyPairName);
                        ECDsaCng ecdsa = new ECDsaCng(clefs);

                        // Open the Windows Certificate Store
                        // Ouverture du magasin de certificats de Windows
                        X509Store CertifStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
                        CertifStore.Open(OpenFlags.ReadWrite);

                        // Merge the SRS certificate with its private key created above and save it in the Windows Certificate Store
                        // Fusion du certificat du SEV avec sa clé privée crée ci-dessus et sauvegarde dans le magasin de certificats Windows
                        X509Certificate2 pfxCert = certSEV.CopyWithPrivateKey(ecdsa);
                        pfxCert.FriendlyName = _SRSCertificatUserFriendlyName + "-" + _KeyPairName;
                        CertifStore.Add(pfxCert);
                        _CertificateSerialNumberSRS = pfxCert.SerialNumber;

                        // WEB-SRM's Certificate
                        // Certificat du MEV-WEB
                        X509Certificate2 certMEVWEB = new X509Certificate2(Convert.FromBase64String(repCertificat.CertificatMEVWEB.Replace("-----BEGIN CERTIFICATE-----", "")
                            .Replace("-----END CERTIFICATE-----", "").Replace(Environment.NewLine, "")), "", X509KeyStorageFlags.PersistKeySet)
                        {
                            FriendlyName = "Certificat MEVWEB"
                        };

                        // Adding the WEB-SRM certificate to the Windows certificate store
                        // Ajout du certificat du MEV-WEB dans le magasin de certificats Windows
                        CertifStore.Add(certMEVWEB);
                        _CertificateSerialNumberWebSRM = certMEVWEB.SerialNumber;

                        // Information received from the WEB-SRM
                        // Informations reçues du MEV-WEB
                        if (_Verbose)
                        {
                            Console.WriteLine(_IDAPPRL);
                            Console.WriteLine(certSEV);
                            Console.WriteLine(certMEVWEB);
                        }

                        CertifStore.Close();
                    }
                    else
                        Console.Error.WriteLine(WEB_SRM_Response.ResponseJsonWEBSRM);
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
            }
        }

        /// <summary>
        /// Show a Certificate request to delete a certificate on the SRS and on the WEB-SRM
        /// Montre les étapes à faire pour supprimer un certificat du SEV et du MEV-WEB
        /// </summary>
        private void DeleteCertificate()
        {
            try
            {
                _CASESSAI = "000.000";

                // Preparation of the json document to send to the WEB-SRM
                // Préparation du document json pour transmission au MEV-WEB
                String jSon = UtilesJSON.GetJsonCertificate(ValeursPossibles.Modif.SUP, null, _CertificateSerialNumberSRS);

                // HTTP Headers
                // Entêtes HTTP
                List<KeyValuePair<String, String>> HTTPHeadersDicti = GetHTTPHeaders();

                // Send to the WEB-SRM the request to delete the certificate
                // Envoi de la requête de suppression du certificat au MEV-WEB
                WEBSRM_Response WEB_SRM_Response = UtilesJSON.CertificatesRequest(ValeursPossibles.Modif.SUP, HTTPHeadersDicti, jSon, _CertificateSerialNumberSRS);

                // HTTP Status codes and description
                if (_Verbose)
                {
                    Console.WriteLine("HTTP Status Code : " + WEB_SRM_Response.StatusCode);
                    Console.WriteLine("HTTP Status Description : " + WEB_SRM_Response.StatusDescription);
                }

                // Processing of error messages received from WEB-SRM, if any
                // Traitement des messages d'erreurs reçus du MEV-WEB, s'il y a lieu
                if (WEB_SRM_Response.ResponseJsonWEBSRM.Contains("listErr"))
                {
                    List<ErrorWEBSRM> lstErr = UtilesJSON.ParseErrors(WEB_SRM_Response.ResponseJsonWEBSRM);
                    if (lstErr.Count > 0)
                        foreach (ErrorWEBSRM er in lstErr)
                        {
                            Console.WriteLine("Id : " + er.Id);
                            Console.WriteLine("Message : " + er.Mess);
                            Console.WriteLine("Code de retour : " + er.CodRetour);
                        }
                }
                else
                {
                    if (!WEB_SRM_Response.ResponseJsonWEBSRM.Contains("Error") && !WEB_SRM_Response.ResponseJsonWEBSRM.Contains("délai d'attente"))
                    {
                        // Open the Windows certificate store and delete the certificate
                        // Ouverture du magasin de certificats de Windows et suppression du certificat
                        X509Store CertifStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
                        CertifStore.Open(OpenFlags.ReadWrite);
                        CertifStore.Remove(Utiles.GetCertificate(_CertificateSerialNumberSRS));
                        CertifStore.Close();
                        _CertificateSerialNumberSRS = "";
                    }
                    else
                        Console.Error.WriteLine(WEB_SRM_Response.ResponseJsonWEBSRM);
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
            }
        }

        /// <summary>
        /// Creation of the Certificate Signing Request
        /// Création du CSR
        /// </summary>
        /// <returns>CSR à transmettre au MEV-WEB</returns>
        private String CertificateCreation()
        {
            // The mandatary must enter the mandatary’s identification number
            // Inscrire le numéro d’identification du mandataire
            String CN = "5678912340";

            // The mandatary must enter the abbreviation for the sector of activity and their authorization code separated by a hyphen. Example : AAA-X9X9-X9X9 Organizational unit
            // Inscrire l’abréviation du secteur d’activité et le code d’autorisation du mandataire reliés par un trait d’union. Exemple : AAA-X9X9-X9X9
            String O = _SECTACTV + "-" + _CODAUTH;

            // The mandatary must enter their QST number. Example : 1234567890TQ0001
            // Inscrire le numéro d’inscription au fichier de la TVQ du mandataire.
            String OU = _noQSTUser;

            // The mandatary must enter a user-friendly name for the certificate (8 to 32 characters per the ASCII  The mandatary must enter a user-friendly name for the certificate (8 to 32 characters per the ASCII standards)
            // Inscrire un nom convivial pour le certificat (8 à 32 caractères selon les normes ASCII).
            String SN = "Certificat A";

            // The mandatary must enter their billing file number. Example : AA9999
            // Inscrire le numéro de dossier relatif à la facturation obligatoire. Exemple : AA9999
            String GN = _NoDossFO;

            // The mandatary must enter the municipality's Coordinated Universal Time (UTC). -04:00 or -05:00
            // Inscrire le temps universel coordonné (UTC) de la municipalité. -04:00 ou -05:00
            String L = Utiles.GetTimeZoneHeure();

            // The mandatary must enter the abbreviation for the province. QC
            // Inscrire l’abréviation de la province. QC
            String S = "QC";

            // The mandatary must enter the abbreviation for the country. CA
            // Inscrire l’abréviation du pays. CA
            String C = "CA";

            try
            {
                // Validations
                UtilesValidation.ValiderCN(CN);
                UtilesValidation.ValiderOU(OU);
                UtilesValidation.ValiderSN(SN);
                UtilesValidation.ValiderGN(GN);
                UtilesValidation.ValiderL(L);
                UtilesValidation.ValiderS(S);
                UtilesValidation.ValiderC(C);

                StringBuilder sbInfoCertificat = new StringBuilder();
                sbInfoCertificat.AppendFormat("CN={0};O={1};SN={2};OU={3};GN={4};L={5};S={6};C={7}", CN, O, SN, OU, GN, L, S, C);

                // CSR preparation and encoding in PEM format
                // Préparation du CSR et encodage au format PEM
                String strCSRPEM = UtilesECDSA.CsrEcdsaPreparation(sbInfoCertificat.ToString(), _KeyPairName);

                return "-----BEGIN CERTIFICATE REQUEST-----\n" + strCSRPEM.Replace(Environment.NewLine, "") + "\n-----END CERTIFICATE REQUEST-----";
            }
            catch (FormatException fe)
            {
                Console.WriteLine(fe);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return null;
        }

        #endregion Certificates

        #region Configuration

        // IDs and other information / HTTP Headers
        // Identifiants et autres informations / Entêtes HTTP
        private static readonly String _ENVIRN = ValeursPossibles.Envirn.DEV;
        private static readonly String _APPRLINIT = ValeursPossibles.ApprlInit.SEV;
        private static readonly String _SECTACTV = ValeursPossibles.Abrvt.RBC;

        private static String _CASESSAI = "000.000";
        private static String _IDAPPRL = "0000-0000-0000";
        private static readonly String _CODCERTIF = "FOB201999999";

        private static readonly String _IDSEV = "000000000000310A";    // Your ID
        private static readonly String _IDVERSI = "00000000000039EE";  // Your ID
        private static readonly String _IDPARTN = "0000000000000886";  // Your ID
        private static readonly String _CODAUTH = "D8T8-W8W8";        // Your Authorization code

        private static readonly String _VERSI = "AC.10001";
        private static readonly String _VERSIPARN = "0";

        internal static readonly bool _Verbose = true;

        // User informations ( not the mandatary )
        // Informations de l'utilisateur ( et non le mandataire )
        private static String _UserName = "Joe Bleau";
        private static String _noGSTUser = "123456789RT0001";
        private static String _noQSTUser = "5678912340TQ0001";
        private static readonly String _NoDossFO = "ER0001";

        #endregion Configuration

        // KeyPair name
        // Nom de la paire de clefs
        private readonly String _KeyPairName = "Black Car 1";

        // User-Friendly name for the SRS certificate
        // Nom Convivial pour le certificat du SEV
        private readonly String _SRSCertificatUserFriendlyName = "Black Car 1";

        // Serial numbers of the SRS's and the WEB-SRM's certificates
        // Numéros de série des certificats du SEV et du MEV-WEB pour identification
        private String _CertificateSerialNumberSRS = "";
        private String _CertificateSerialNumberWebSRM = "";

        /// <summary>
        /// Show the steps to do
        /// Montre les étapes à effectuer
        /// </summary>

        #region Private Methods
        /// <summary>
        /// Préparation de la liste des entêtes HTTP pour la connexion au MEV-WEB
        /// Preparation of the list of HTTP headers for the connection to the WEB-SRM
        /// </summary>
        /// <returns>Liste des entêtes</returns>
        private List<KeyValuePair<String, String>> GetHTTPHeaders()
        {
            List<KeyValuePair<String, String>> HTTPHeadersDicti = new List<KeyValuePair<String, String>>();
            try
            {
                HTTPHeadersDicti.Add(new KeyValuePair<String, String>("CONTENT-TYPE", "application/json"));

                if (UtilesValidation.ValiderENVIRN(_ENVIRN))
                    HTTPHeadersDicti.Add(new KeyValuePair<String, String>("ENVIRN", _ENVIRN));

                if (UtilesValidation.ValiderCASESSAI(_CASESSAI))
                    HTTPHeadersDicti.Add(new KeyValuePair<String, String>("CASESSAI", _CASESSAI));

                if (UtilesValidation.ValiderAPPRLINIT(_APPRLINIT))
                    HTTPHeadersDicti.Add(new KeyValuePair<String, String>("APPRLINIT", _APPRLINIT));

                if (UtilesValidation.ValiderIDAPPRL(_IDAPPRL))
                    HTTPHeadersDicti.Add(new KeyValuePair<String, String>("IDAPPRL", _IDAPPRL));

                if (UtilesValidation.ValiderIDSEV(_IDSEV))
                    HTTPHeadersDicti.Add(new KeyValuePair<String, String>("IDSEV", _IDSEV));

                if (UtilesValidation.ValiderIDVERSI(_IDVERSI))
                    HTTPHeadersDicti.Add(new KeyValuePair<String, String>("IDVERSI", _IDVERSI));

                if (UtilesValidation.ValiderCODCERTIF(_CODCERTIF))
                    HTTPHeadersDicti.Add(new KeyValuePair<String, String>("CODCERTIF", _CODCERTIF));

                if (UtilesValidation.ValiderIDPARTN(_IDPARTN))
                    HTTPHeadersDicti.Add(new KeyValuePair<String, String>("IDPARTN", _IDPARTN));

                if (UtilesValidation.ValiderVERSI(_VERSI))
                    HTTPHeadersDicti.Add(new KeyValuePair<String, String>("VERSI", _VERSI));

                if (UtilesValidation.ValiderVERSIPARN(_VERSIPARN))
                    HTTPHeadersDicti.Add(new KeyValuePair<String, String>("VERSIPARN", _VERSIPARN));

            }
            catch (FormatException fe)
            {
                Console.WriteLine(fe);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return HTTPHeadersDicti;
        }
        #endregion Private Methods

        public class ReqCertif
        {
            public string modif { get; set; }
            public string csr { get; set; }
        }

        public class RequestData
        {
            public string ENVIRN { get; set; }
            public string CASESSAI { get; set; }
            public string APPRLINIT { get; set; }
            public string IDAPPRL { get; set; }
            public string IDSEV { get; set; }
            public string IDVERSI { get; set; }
            public string CODCERTIF { get; set; }
            public string IDPARTN { get; set; }
            public string VERSI { get; set; }
            public int VERSIPARN { get; set; }
            public ReqCertif reqCertif { get; set; }
        }

        private void BtnClearResponse_Click(object sender, EventArgs e)
        {
            txtResponse.Text = "";
        }

        private void BtnDeleteCertificate_Click(object sender, EventArgs e)
        {
            DeleteCertificate();
        }
    }
}

Since a week, I try so many solution, I was able to succeed using Microsoft but the API creator told me that I must use RestSharp

0 Answers0