Skip to content

Instantly share code, notes, and snippets.

@rsoeteman
Created February 28, 2022 10:13
Show Gist options
  • Save rsoeteman/2b59b87f4eb15072166e49855538d64c to your computer and use it in GitHub Desktop.
Save rsoeteman/2b59b87f4eb15072166e49855538d64c to your computer and use it in GitHub Desktop.
Use V2.x Package Garden Licensing
public sealed class ProductLicense
{
private readonly IHostingEnvironment _hostingEnvironment;
private readonly IHttpContextAccessor _contextAccessor;
private readonly ILogger<ProductLicense> _logger;
public ProductLicense(IHostingEnvironment hostingEnvironment, IHttpContextAccessor contextAccessor, ILogger<ProductLicense> logger)
{
_hostingEnvironment = hostingEnvironment;
_contextAccessor = contextAccessor;
_logger = logger;
}
private static Validator _validator;
private static readonly object _lock = new object();
//Change this to your own contact information. This message will be used in exceptions
private const string LicenseSupportInfo =
"Please contact Soeteman Software support on [email protected]";
//The productkey generated by the license generator package
private const string ProductKey =
"My secret product key goes here";
//The productParameters generated by the license generator package
private const string ProductParameters =
"My secret parameters go here";
//The major version of the package
private const int ProductVersion = 9;
//The path to the license file
public const string LicensefilePaths = "/bin/cmsimport.lic,/umbraco/Licenses/cmsimport.lic";
/// <summary>
/// Singleton instance to the validator
/// </summary>
public Validator Instance
{
get
{
lock (_lock)
{
if (_validator == null)
{
var file = FindFile();
using (var fs = File.Exists(file) ? File.OpenRead(file) : FileStream.Null)
{
_validator = new Validator(_contextAccessor, ProductParameters, ProductKey, fs,
LicenseSupportInfo, ProductVersion);
return _validator;
}
}
return _validator;
}
}
}
private string FindFile()
{
var file = string.Empty;
foreach (var licensefilePath in LicensefilePaths.Split(','))
{
file = _hostingEnvironment.MapPathContentRoot(licensefilePath);
_logger.LogDebug($"Looking for a license at {file}");
if (File.Exists(file))
{
//License file is found in location
return file;
}
}
return file;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment