// update  more info For The Certificate
        public int UpdateCertificateInfo(string Cert, int reqid)
        {
            StreamWriter objFile = null;

            try
            {
                objFile = File.CreateText("c:\\" + reqid + ".cer");
                objFile.Write(Cert);
                objFile.Close();
                X509Certificate2 cert = new X509Certificate2("c:\\" + reqid + ".cer");
                File.Delete("c:\\" + reqid + ".cer");

                using (caProjectEntities context = new caProjectEntities())
                {
                    cert cetificate = context.certs.FirstOrDefault(r => r.RequestId == reqid);
                    cetificate.ExpiredDate  = Convert.ToDateTime(cert.NotAfter.ToString("yyyy/MM/dd hh:mm:ss tt"));
                    cetificate.Issuedby     = cert.Issuer.ToString();
                    cetificate.serialnumber = cert.GetSerialNumberString();
                    context.SaveChanges();
                }
                return(0);
            }
            catch (Exception ex)
            {
                if (objFile != null)
                {
                    objFile.Close();
                }
                Console.Write(ex.Message);
                return(-1);
            }
        }
 //delete cert record for expired certificate
 public void DeleteCertificateRecordFromDb(int reqid)
 {
     using (caProjectEntities context = new caProjectEntities())
     {
         cert cetificate = context.certs.FirstOrDefault(r => r.RequestId == reqid);
         context.certs.Remove(cetificate);
         context.SaveChanges();
     }
 }
 //update certFlag for unlock
 public void UnlockCetificate(string hostname)
 {
     using (caProjectEntities context = new caProjectEntities())
     {
         cert cetificate = context.certs.FirstOrDefault(r => r.HostName == hostname);
         cetificate.CertFlag = null;
         context.SaveChanges();
     }
 }
 //update Certification Status and cert Flag
 public void UpdateUnlockFlagAndStatus(int status, int reqid)
 {
     using (caProjectEntities context = new caProjectEntities())
     {
         cert cetificate = context.certs.FirstOrDefault(r => r.RequestId == reqid);
         cetificate.status = status;
         if (status == 3) //issued
         {
             cetificate.CertFlag = "true";
         }
         context.SaveChanges();
     }
 }
 // Inserts First info For The Certificate
 public void   InsertToCertificateTable(string hostname, int status, int reqid)
 {
     using (caProjectEntities context = new caProjectEntities())
     {
         cert cetificate = new cert()
         {
             CreationDate = DateTime.Now.ToString("yyyyMMdd", System.Globalization.CultureInfo.GetCultureInfo("en-US")),
             RequestId    = reqid,
             HostName     = hostname,
             status       = status
         };
         context.certs.Add(cetificate);
         context.SaveChanges();
     }
 }
 // Inserts Error Message
 public void InsertToErrorMessageTable(string hostname, int requestid, string errorMessage, string functionName)
 {
     using (caProjectEntities context = new caProjectEntities())
     {
         Error_Log erroLog = new Error_Log()
         {
             ErrorMessage = errorMessage,
             Request      = requestid,
             CreateDate   = DateTime.Now.ToString(),
             HostName     = hostname,
             functionName = functionName
         };
         context.Error_Logs.Add(erroLog);
         context.SaveChanges();
     }
 }
 // Inserts First info For The Certificate
 public void InsertToSigntureTable(string ClientId, string Hash, string Username)
 {
     using (caProjectEntities context = new caProjectEntities())
     {
         DateTime  dt        = DateTime.Now;
         signature signature = new signature()
         {
             hash      = Hash,
             uuid      = ClientId,
             username  = Username,
             timestamp = BitConverter.GetBytes(dt.Ticks),
             ldtdisk   = "N"
         };
         context.signatures.Add(signature);
         context.SaveChanges();
     }
 }