public async Task <IActionResult> Upload(IFormFile upload, string documentaction) { Document document = await FileManager.StoreFile(upload); if (document == null) { return(View("InvalidDocument")); } if (documentaction == "Sign") { var x509certificate = HttpContext.Connection.ClientCertificate; if (x509certificate == null) { return(View("OperationNotAllowed")); } var certificate = await DBContext.Certificate. SingleOrDefaultAsync(r => r.SerialNumber == x509certificate.SerialNumber); if (certificate == null) { return(View("OperationNotAllowed")); } if (certificate.ExpireDate < DateTime.Now) { certificate.Revoked = true; certificate.RevokeDate = DateTime.UtcNow.Date; DBContext.Certificate.Update(certificate); await DBContext.SaveChangesAsync(); } if (certificate.ExpireDate < DateTime.Now || certificate.Revoked == true) { return(RedirectToAction("CertificateExpired", "Certificates")); } document.ReviewerUuid = certificate.ReviewerUuid; document.Reviewer = certificate.Reviewer; document.Signed = false; DBContext.Add(document); await DBContext.SaveChangesAsync(); return(View("DocumentSign", document)); } else if (documentaction == "Verify") { DBContext.Add(document); await DBContext.SaveChangesAsync(); return(RedirectToAction("VerifyDocument", new { uuid = document.Uuid })); } return(View("OperationNotAllowed")); }
public async Task <IActionResult> Create([Bind("Uuid,Email,Name,Title,Role")] Reviewer reviewer) { if (ModelState.IsValid) { reviewer.Uuid = Guid.NewGuid().ToString(); _context.Add(reviewer); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(reviewer)); }
public async Task <IActionResult> Issue( string requestId, string password, string code) { if (string.IsNullOrEmpty(requestId)) { return(NotFound()); } var request = await DBContext.CertificateRequest .Include(s => s.Reviewer) .SingleOrDefaultAsync(m => m.Uuid == requestId); if (request == null) { return(NotFound()); } if (code != request.SecurityCode) { return(RedirectToAction("RequestExpired")); } var now = DateTime.UtcNow.Date; var certificate = new Certificate(); certificate.Uuid = Guid.NewGuid().ToString(); certificate.CreationDate = now; certificate.ExpireDate = now.AddYears(1); certificate.Revoked = false; certificate.ReviewerUuid = request.ReviewerUuid; certificate.ReviewerName = request.Reviewer.Name; certificate.ReviewerEmail = request.Reviewer.Email; DistinguishedName dn = new DistinguishedName(); dn.CommonName = request.Reviewer.Name; dn.Email = request.Reviewer.Email; dn.Organization = TrustManager.IssuerDN.Organization; dn.OrganizationalUnit = TrustManager.IssuerDN.OrganizationalUnit; dn.Country = TrustManager.IssuerDN.Country; dn.Locality = TrustManager.IssuerDN.Locality; dn.State = TrustManager.IssuerDN.State; var x509certificate = TrustManager.IssueCertificate( certificate.Uuid.ToString(), password, dn, CertificateType.ReviewerCertificate, now, now.AddYears(1)); certificate.SerialNumber = x509certificate.SerialNumber; DBContext.Add(certificate); await DBContext.SaveChangesAsync(); request.CertificateUuid = certificate.Uuid; request.Certificate = certificate; request.Reviewer.Certificate = certificate; DBContext.Update(request); await DBContext.SaveChangesAsync(); var message = await RenderService.RenderToStringAsync("Email/CertificateIssued", request.Reviewer); var attachments = new List <Attachment> (); attachments.Add( await EmailManager.LoadAttachment( TrustManager.CertificatePath( certificate.Uuid, CertificateType.ReviewerCertificate, StoreFormat.P12Store), "private.p12", "application/x-pkcs12")); attachments.Add( await EmailManager.LoadAttachment( TrustManager.CertificatePath( certificate.Uuid, CertificateType.ReviewerCertificate, StoreFormat.CRT), "public.crt", "application/x-x509-ca-cert")); attachments.Add( await EmailManager.LoadAttachment( TrustManager.CertificatePath( "root", CertificateType.AuthorityCertificate, StoreFormat.CRT), "authority.crt", "application/x-x509-ca-cert")); var response = await EmailManager.SendEmailHTML( message, EmailManager.Sender, request.Reviewer.Email, "Your new certificate is ready", attachments ); if (!response.Successful) { return(View("ErrorSendingCertificate")); } return(RedirectToAction(nameof(CertificateSent))); }