I try to call this function:
GenerateSSL("co name", "IT", "Stockholm", "Stockholm", "US", "email@gmail.com", "my-domain.com" , "pass word");
public static async Task GenerateSSL(
string companyName,
string organizationalUnit,
string locality,
string state,
string countryCode,
string email,
string domain,
string password)
{
IList< string> domains = new[] { domain };
var acme = new AcmeContext(WellKnownServers.LetsEncryptStagingV2);
var account = await acme.NewAccount("myemail@gmail.com", true);
Console.WriteLine(acme.AccountKey.ToPem());
// Save the account key for later use
//var pemKey = acme.AccountKey.ToPem();
var order = await acme.NewOrder(domains);
var authorizations = await order.Authorizations();
//get all of the authorizations
foreach (var auths in authorizations)
{
var httpChallenge = await auths.Http();
var dnsChallenge = await auths.Dns();
var dnsTxt = acme.AccountKey.DnsTxt(httpChallenge.Token);
//Set DNS settings
Console.WriteLine(dnsTxt);
var title = acme.AccountKey.DnsTxt(httpChallenge.Token);
var keyAuthz = acme.AccountKey.KeyAuthorization(httpChallenge.KeyAuthz);
string folder =
@"C:\wwwroot\my-domain.com\wwwroot\.well-known\acme-challenge\";
File.WriteAllText(folder + httpChallenge.Token, httpChallenge.KeyAuthz);
await httpChallenge.Validate();
var resource = await auths.Resource();
while (resource.Status != AuthorizationStatus.Valid)
{
await Task.Delay(1000);
if (resource.Status == AuthorizationStatus.Invalid)
{
Console.WriteLine("Invalid!");
Console.ReadLine();
break;
}
}
}
Console.WriteLine("valid!");
//create the private key
var privateKey = KeyFactory.NewKey(KeyAlgorithm.ES256);
var csr = new CertificationRequestBuilder();
string commonNames = string.Join(",", domains.Select(o => $"CN={o}"));
string subject = $"C={countryCode}, ST={state}, L={locality}, O={companyName}, OU={organizationalUnit}, {commonNames}, emailAddress={email}\"";
csr.AddName(subject);
foreach (var dom in domains)
csr.SubjectAlternativeNames.Add(dom);
//get the cert(s)
await order.Finalize(csr.Generate());
var cert = await order.Download();
var certPem = cert.ToPem();
var pfxBuilder = cert.ToPfx(privateKey);
var pfx = pfxBuilder.Build(domains.First(), password);
System.IO.File.WriteAllBytes($@"C:\temp\cert\output1.pfx", pfx);
// Assume 'site' is already set to your site via something like
// Site site = mgr.Sites.Add(siteName, directory, 443);
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);
// Here, directory is my install dir, and (directory)\bin\certificate.pfx is where the cert file is.
// 1234 is the password to the certfile (exported from IIS)
X509Certificate2 certificate = new X509Certificate2( @"C:\temp\cert\output1.pfx", "dsadsadsa");
store.Add(certificate);
Microsoft.Web.Administration.ServerManager iisManager = new Microsoft.Web.Administration.ServerManager();
if (iisManager.Sites[domain] == null)
return;
iisManager.Sites[domain].Bindings.Add("*:443:", certificate.GetCertHash(), store.Name);
iisManager.CommitChanges();
}
I able to see "valid" result, but ".pfx" has problem. This error appears during using ".pfx" file:
The specified file could not be decrypted