예제 #1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                LoadData();
                return(Page());
            }

            if (AcmeCertificate.KeyId < 0)
            {
                var key = _keyGenerator.Generate(AcmeCertificate.Subject, KeyAlgorithm.RS256);
                if (key == null)
                {
                    ModelState.AddModelError(string.Empty, "Error creating key");
                    return(Page());
                }

                AcmeCertificate.KeyId = key.KeyId;
            }

            AcmeCertificate.ApiKey1 = ApiKeyGenerator.CreateApiKey();
            AcmeCertificate.ApiKey2 = ApiKeyGenerator.CreateApiKey();

            _context.AcmeCertificates.Add(AcmeCertificate);
            await _context.SaveChangesAsync();

            StatusMessage = "Certificate created";

            return(RedirectToPage("./Index"));
        }
예제 #2
0
        public async Task <IActionResult> OnPostAsync(long?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            AcmeCertificate = await _context.AcmeCertificates
                              .Include(a => a.AcmeAccount)
                              .Include(a => a.Key)
                              .FirstOrDefaultAsync(m => m.AcmeCertificateId == id);

            if (AcmeCertificate == null)
            {
                return(NotFound());
            }
            // Prevent deleting of site certificate
            if (AcmeCertificate.Subject == _httpServerOptions.Value.SiteHostname)
            {
                StatusMessage = "Cannot delete site certificate";
                return(Page());
            }

            _context.AcmeCertificates.Remove(AcmeCertificate);
            await _context.SaveChangesAsync();

            StatusMessage = "Certificate deleted";

            return(RedirectToPage("./Index"));
        }
예제 #3
0
        public async Task <IActionResult> OnPostAsync(long?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Key = await _context.Keys.FindAsync(id);

            if (Key != null)
            {
                _context.Keys.Remove(Key);
                try
                {
                    await _context.SaveChangesAsync();

                    StatusMessage = "Key deleted";
                }
                catch (DbUpdateException)
                {
                    StatusMessage = "Unable to delete key in use";
                    return(Page());
                }
            }

            return(RedirectToPage("./Index"));
        }
예제 #4
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Attach(AcmeAccount).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();

                StatusMessage = "Account updated";
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AcmeAccountExists(AcmeAccount.AcmeAccountId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
예제 #5
0
        public async Task <IActionResult> OnGetAsync()
        {
            var userId = long.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier));

            NotificationSetting = await _context.NotificationSettings.FirstOrDefaultAsync(m => m.ApplicationUserId == userId);

            if (NotificationSetting == null)
            {
                NotificationSetting = new NotificationSetting
                {
                    ApplicationUserId = userId
                };
                _context.NotificationSettings.Add(NotificationSetting);
                await _context.SaveChangesAsync();
            }
            return(Page());
        }
예제 #6
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            if (!AcmeCertificateExists(AcmeCertificate.AcmeCertificateId))
            {
                return(NotFound());
            }

            var cert = await _context.AcmeCertificates
                       .FirstOrDefaultAsync(x => x.AcmeCertificateId == AcmeCertificate.AcmeCertificateId);

            // Prevent changing the subject for this site's certificate
            if (cert.Subject == _httpServerOptions.Value.SiteHostname &&
                !string.Equals(AcmeCertificate.Subject, cert.Subject, StringComparison.OrdinalIgnoreCase))
            {
                ModelState.AddModelError(string.Empty, "Cannot change site certificate subject");
                return(Page());
            }

            cert.CsrCommonName       = AcmeCertificate.CsrCommonName;
            cert.CsrCountryName      = AcmeCertificate.CsrCountryName;
            cert.CsrLocality         = AcmeCertificate.CsrLocality;
            cert.CsrOrganization     = AcmeCertificate.CsrOrganization;
            cert.CsrOrganizationUnit = AcmeCertificate.CsrOrganizationUnit;
            cert.CsrState            = AcmeCertificate.CsrState;
            cert.CsrState            = AcmeCertificate.CsrState;
            cert.Name          = AcmeCertificate.Name;
            cert.KeyId         = AcmeCertificate.KeyId;
            cert.Subject       = AcmeCertificate.Subject;
            cert.SANs          = AcmeCertificate.SANs;
            cert.ChallengeType = AcmeCertificate.ChallengeType;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AcmeCertificateExists(AcmeCertificate.AcmeCertificateId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
예제 #7
0
        public async Task <IActionResult> OnPostAsync(IFormFile keyFile, int keyAlgorithm)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            IKey key = null;

            // First check the plain text uploaded PEM encoded certificate (if any)
            if (!string.IsNullOrWhiteSpace(Key.RawData))
            {
                try
                {
                    key = KeyFactory.FromPem(Key.RawData);
                }
                catch (Exception) { }
            }

            // If key is still null, check the uploaded key contents
            if (key == null)
            {
                var keyFileContents = await keyFile.ReadAsBytesAsync();

                if (keyFileContents != null)
                {
                    // Check if it's DER encoded
                    try
                    {
                        key = KeyFactory.FromDer(keyFileContents);
                    }
                    catch (Exception) { }

                    if (key == null)
                    {
                        // How about PEM?
                        var keyPem = Encoding.UTF8.GetString(keyFileContents);

                        try
                        {
                            key = KeyFactory.FromPem(keyPem);
                        }
                        catch (Exception) { }
                    }

                    if (key != null)
                    {
                        Key.RawData = key.ToPem();
                    }
                }
            }
            var validKeyAlgValues = (int[])Enum.GetValues(typeof(KeyAlgorithm));

            if (key == null && validKeyAlgValues.Contains(keyAlgorithm))
            {
                var keyAlg = (KeyAlgorithm)keyAlgorithm;
                _keyGenerator.Generate(Key.Name, keyAlg, Key.Description);
                return(RedirectToPage("./Index"));
            }

            if (key == null)
            {
                ModelState.AddModelError(string.Empty, "You must pick a key algorithm, enter in the PEM or upload a key");
                return(Page());
            }

            Key.ApiKey1 = ApiKeyGenerator.CreateApiKey();
            Key.ApiKey2 = ApiKeyGenerator.CreateApiKey();

            _context.Keys.Add(Key);
            await _context.SaveChangesAsync();

            StatusMessage = "Key created";

            return(RedirectToPage("./Index"));
        }