Skip to content

Instantly share code, notes, and snippets.

@HowardvanRooijen
Last active August 29, 2015 14:16
Show Gist options
  • Save HowardvanRooijen/5973a9bd8e0e0a64d5ca to your computer and use it in GitHub Desktop.
Save HowardvanRooijen/5973a9bd8e0e0a64d5ca to your computer and use it in GitHub Desktop.
Validate the License Logic
private static void ValidateLicense(string publicKeyPath, string licensePath)
{
var publicKey = new PublicCryptoKey { Contents = File.ReadAllText(publicKeyPath) };
var clientLicense = ClientLicense.Create(File.ReadAllText(licensePath));
var violations = new List<string>();
try
{
var licenseValidationRules = new List<ILicenseValidationRule>
{
new LicenseHasNotExpiredRule(),
new ValidNumberOfCoresLicenseRule()
};
new LicenseValidator().Validate(clientLicense, publicKey, licenseValidationRules);
}
catch (InvalidLicenseException exception)
{
violations.Add(exception.Message);
}
catch (AggregateException ex)
{
var innerExceptions = ex.InnerExceptions;
foreach (var exception in innerExceptions)
{
if (exception is LicenseViolationException)
{
violations.Add(exception.Message);
}
}
// If you've got to this point and there are no violations,
// something very undesirable is happening, so bubble it up.
if (!violations.Any())
{
throw;
}
}
catch (Exception)
{
violations.Add(Messages.UnknownLicenseError);
}
if (violations.Any())
{
Console.WriteLine(Messages.LicenseViolationsEncountered);
foreach (var violation in violations)
{
Console.WriteLine(" - " + violation);
}
Console.WriteLine(Messages.PressAnyKey);
Console.ReadKey();
Environment.Exit(-1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment