コード例 #1
0
        public async Task <IActionResult> OnPostAsync()
        {
            // Check basic validation first and bail out early before anything gets updated.
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            if (Uri.CheckHostName(Setup.SiteHostname) != UriHostNameType.Dns)
            {
                ModelState.AddModelError("Setup.Domain", "Invalid domain name");
            }

            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _httpServerOptions.Update(x =>
            {
                x.SiteHostname = Setup.SiteHostname;
                x.HttpsPort    = Setup.HttpsPort;
            });

            var acmeAccount = await _dataContext.AcmeAccounts.FirstAsync(x => x.IsAcmeStaging == Setup.UseAcmeStaging);

            var acmeCert = await _dataContext.AcmeCertificates
                           .Include(x => x.AcmeAccount)
                           .ThenInclude(x => x.Key)
                           .FirstOrDefaultAsync(x => x.Subject == Setup.SiteHostname &&
                                                x.AcmeAccountId == acmeAccount.AcmeAccountId);

            if (acmeCert == null)
            {
                var certKey = await _dataContext.Keys.FirstOrDefaultAsync(x => x.Name == Setup.SiteHostname);

                if (certKey == null)
                {
                    certKey = _keyGenerator.Generate(Setup.SiteHostname, Certes.KeyAlgorithm.RS256,
                                                     "certera certificate (this site)");
                }

                acmeCert = new Data.Models.AcmeCertificate
                {
                    ChallengeType = "http-01",
                    DateCreated   = DateTime.UtcNow,
                    Name          = Setup.SiteHostname,
                    Subject       = Setup.SiteHostname,
                    AcmeAccountId = acmeAccount.AcmeAccountId,
                    KeyId         = certKey.KeyId
                };
                _dataContext.AcmeCertificates.Add(acmeCert);
                await _dataContext.SaveChangesAsync();
            }

            return(RedirectToPage("./Certificate"));
        }
コード例 #2
0
        public void Initialize(Data.Models.AcmeCertificate acmeCert)
        {
            _acmeCertificate = acmeCert;

            IKey accountKey = KeyFactory.FromPem(acmeCert.AcmeAccount.Key.RawData);

            _acmeContext = new AcmeContext(acmeCert.AcmeAccount.IsAcmeStaging
                ? WellKnownServers.LetsEncryptStagingV2
                : WellKnownServers.LetsEncryptV2, accountKey);
        }