public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } bool deleteOnError = false; if (AcmeAccount.KeyId < 0) { var stg = AcmeAccount.IsAcmeStaging ? "-staging" : string.Empty; var keyName = $"acme-account{stg}"; var key = _keyGenerator.Generate(keyName, KeyAlgorithm.ES256); if (key == null) { ModelState.AddModelError(string.Empty, "Error creating key"); return(Page()); } deleteOnError = true; AcmeAccount.Key = key; } AcmeAccount.ApplicationUser = await _userManager.GetUserAsync(User); try { _context.AcmeAccounts.Add(AcmeAccount); await _context.SaveChangesAsync(); // Create account with key await _certesAcmeProvider.CreateAccount(AcmeAccount.AcmeContactEmail, AcmeAccount.Key.RawData, AcmeAccount.IsAcmeStaging); } catch (Exception e) { // Delete created key if there's a failure in creating the acme account if (deleteOnError) { _context.Keys.Remove(AcmeAccount.Key); await _context.SaveChangesAsync(); } _logger.LogError(e, "Error creating ACME account"); ModelState.AddModelError(string.Empty, "Error creating ACME account"); return(Page()); } StatusMessage = "Account created"; return(RedirectToPage("./Index")); }
private async Task EnsureLetsEncryptAccountExists(Data.Models.AcmeAccount acmeAccount, bool staging) { var accountExists = await _certesAcmeProvider.AccountExists(acmeAccount.Key.RawData, staging); if (accountExists) { _logger.LogDebug("ACME account already exists."); } else { _logger.LogDebug("ACME account does not exists, creating account using existing key."); // Create account with existing key await _certesAcmeProvider.CreateAccount(acmeAccount.AcmeContactEmail, acmeAccount.Key.RawData, staging); } }