Standard.Licensing
Standard.Licensing copied to clipboard
Expiration Date is not correctly compared
The expiration date is set as Universal date when creating the License file (ToUniversalTime()).
But the LicenseValidationDate extension method ExpirationDate is comparing with DateTime.Now.
Shouldn't they both be consistently UniversalTime?
The Expiration property is in UTC. The ExpirationDate() method compares the UTC value of Expiration with the local DateTime.Now value.
The workaround is to use the second ExpirationDate(DateTime) method and pass a UTC value.
Personally, I think Expiration should be a DateOnly and the comparison should be against the local date.
Another issue is that when the Expiration property parses the DateTime value from the XML license file, the Kind property is left as Local. The ParseExact() method needs to add the DateTimeStyles.AdjustToUniversal style.
public ILicenseBuilder ExpiresAt(DateTime date)
{
license.Expiration = date.ToUniversalTime();
return this;
}
public static IValidationChain ExpirationDate(this IStartValidationChain validationChain)
{
return ExpirationDate(validationChain, DateTime.Now);
}
public static IValidationChain ExpirationDate(this IStartValidationChain validationChain, DateTime systemDateTime)
{
var validationChainBuilder = (validationChain as ValidationChainBuilder);
var validator = validationChainBuilder.StartValidatorChain();
validator.Validate = license => license.Expiration > systemDateTime;
validator.FailureResult = new LicenseExpiredValidationFailure()
{
Message = "Licensing for this product has expired!",
HowToResolve = @"Your license is expired. Please contact your distributor/vendor to renew the license."
};
return validationChainBuilder;
}
I have created PR #47 to fix this.