public async Task <IActionResult> Edit(string uuid, [Bind("Uuid,Email,Name,Title,Role")] Reviewer reviewer)
        {
            if (uuid != reviewer.Uuid)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(reviewer);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ReviewerExists(reviewer.Uuid))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(reviewer));
        }
Exemple #2
0
        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)));
        }