/// <summary> /// Validates the license. /// </summary> /// <param name="license">The license key the user has given to be validated.</param> /// <returns>The validation context containing all the validation results.</returns> public IValidationContext ValidateLicense(string license) { Argument.IsNotNullOrWhitespace(() => license); var validationContext = new ValidationContext(); Log.Info("Validating license"); try { var licenseObject = License.Load(license); var failureList = licenseObject.Validate() .Signature(_applicationIdService.ApplicationId) .AssertValidLicense().ToList(); if (failureList.Count > 0) { foreach (var failure in failureList) { var businessRuleValidationResult = BusinessRuleValidationResult.CreateErrorWithTag(failure.Message, failure.HowToResolve); validationContext.AddBusinessRuleValidationResult(businessRuleValidationResult); } } var licenseAttributes = licenseObject.AdditionalAttributes; if (licenseAttributes != null) { foreach (var licenseAttribute in licenseAttributes.GetAll()) { if (string.Equals(licenseAttribute.Key, LicenseElements.MachineId)) { Log.Debug("Validating license using machine ID"); var machineLicenseValidationContext = _machineLicenseValidationService.Validate(licenseAttribute.Value); validationContext.SynchronizeWithContext(machineLicenseValidationContext, true); if (machineLicenseValidationContext.HasErrors) { Log.Error("The license can only run on machine with ID '{0}'", licenseAttribute.Value); } } // TODO: add additional attribute checks here } } // Also validate the xml, very important for expiration date and version var xmlValidationContext = ValidateXml(license); validationContext.SynchronizeWithContext(xmlValidationContext, true); } catch (Exception ex) { Log.Error(ex, "An error occurred while loading the license"); validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError("An unknown error occurred while loading the license, please contact support")); } finally { if (validationContext.GetErrors().Count > 0) { Log.Warning("License is not valid:"); Log.Indent(); foreach (var error in validationContext.GetErrors()) { Log.Warning("- {0}\n{1}", error.Message, error.Tag as string); } Log.Unindent(); } else { Log.Info("License is valid"); } } return validationContext; }