Created
August 8, 2016 07:29
-
-
Save nul800sebastiaan/4999468e3097e2147d54c06daa8ffb26 to your computer and use it in GitHub Desktop.
CreateCertificate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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