Instantly share code, notes, and snippets.

Embed
What would you like to do?
CreateCertificate
using System;
using System.IO;
using System.Web.Hosting;
using System.Web.Http;
using Umbraco.Core.Logging;
using Umbraco.Web.WebApi;
namespace Cork.Core.Controllers
{
public class CertificateController : UmbracoApiController
{
[HttpPost]
public string ValidationFile(ValidationFile model)
{
try
{
CreateValidationFile(model);
}
catch (Exception ex)
{
var returnMessage = "Error writing the validation file, exiting";
LogHelper.Error<CertificateController>(returnMessage, ex);
return returnMessage;
}
return "http://cork.nl/.well-known/acme-challenge/" + model.DirectoryName + "/index.html";
}
private static void CreateValidationFile(ValidationFile model)
{
var wellKnownDirectory = HostingEnvironment.MapPath("~\\.well-known");
if (wellKnownDirectory != null && Directory.Exists(wellKnownDirectory) == false)
Directory.CreateDirectory(wellKnownDirectory);
var acmeChallengeDirectory = wellKnownDirectory + "\\acme-challenge";
if (Directory.Exists(acmeChallengeDirectory) == false)
Directory.CreateDirectory(acmeChallengeDirectory);
var challengeDirectory = acmeChallengeDirectory + "\\" + model.DirectoryName;
if (Directory.Exists(challengeDirectory) == false)
Directory.CreateDirectory(challengeDirectory);
File.WriteAllText(challengeDirectory + "\\index.html", model.FileContent);
}
}
public class ValidationFile
{
public string DirectoryName { get; set; }
public string FileContent { get; set; }
public string Token { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment