Standard.Licensing icon indicating copy to clipboard operation
Standard.Licensing copied to clipboard

Expiration Date is not correctly compared

Open psovit opened this issue 5 years ago • 2 comments

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?

psovit avatar Aug 13 '20 11:08 psovit

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;
        }

skst avatar Feb 23 '25 23:02 skst

I have created PR #47 to fix this.

skst avatar Feb 27 '25 18:02 skst