junian / Standard.Licensing

Easy-to-use licensing library for .NET Framework, Mono, .NET Core, and MAUI / Xamarin products
https://junian.dev/Standard.Licensing/
MIT License
535 stars 120 forks source link

validationFailures.ToList() not working #12

Open Manfrons opened 4 years ago

Manfrons commented 4 years ago

It seems that after iterate over the failures collection, calling ToList() or ToArray() doesn't populate it again. You can test with this code. I've generated an xml license file (copy/paste from main page), changed the expiration date inside the xml file and called the verification. Any returns true, but the foreach is skipped.

if (validationFailures.Any()) { validationFailures.ToList(); foreach (var failure in validationFailures.ToList()) list.Items.Add(failure.GetType().Name + ": " + failure.Message + " - " + failure.HowToResolve); } else list.Items.Add("All ok");

sylvainonweb commented 4 years ago

Hi @Manfrons, I also encountered this errror and the solution was to call validationFailures.ToList() to retrieve the failures. This was written on the README.MD page.

Make sure to call validationFailures.ToList() or validationFailures.ToArray() before using the result multiple times.

            var failures = validationFailures.ToList();
            if (failures.Count > 0)
            {
                StringBuilder builder = new StringBuilder();
                foreach (var failure in failures)
                {
                    builder.AppendLine(failure.GetType().Name + ": " + failure.Message + " - " + failure.HowToResolve);
                }
            }
dragonfly22000 commented 4 years ago

Hello, i have the same problem..... What i do: StringBuilder sb = new StringBuilder(); var license = Standard.Licensing.License.Load(text); var validationFailures = license.Validate() .ExpirationDate() .When(lic => lic.Type == LicenseType.Trial) .And() .Signature(publicKey) .AssertValidLicense(); var failures = validationFailures.ToList(); if (failures.Count > 0) { StringBuilder builder = new StringBuilder(); foreach (var failure in failures) { builder.AppendLine(failure.GetType().Name + ": " + failure.Message + " - " + failure.HowToResolve); } }

When i change date in License file to have a failure i have an exception before putting something in failures list.... Help me please,thanks a lot.

sylvainonweb commented 4 years ago

@dragonfly22000 it seems to be because the date you entered isn't a valid date. On my side I tried with a file like this

<License>
  <Id>5f39c964-d48e-4da0-a06f-18fed6c8fb9c</Id>
  <ProductFeatures>
    <Feature name="XXXX">true</Feature>
  </ProductFeatures>
  <Customer>
    <CustomerData name="Name">YYY</CustomerData>
  </Customer>
  <Expiration>Sat, 06 Jun 2020 17:03:53 GMT</Expiration>
  <Signature>MEUCIQCqiackGvfmsEviUw9BN7v9bkFFktIrOORlUyEalXhWkAIgIz24qoAbAC9S1dQEswK92jC/kHYLV73dDcG6yB2ZprY=</Signature>
</License>

1) I change the date for something like this Sat, 13 Jun 2020 17:03:53 GMT so

InvalidSignatureValidationFailure: License signature validation error! - The license signature and data does not match. This usually happens when a license file is corrupted or has been altered.

2) I change the date for something like this Sat, 06 Jun 2021 17:03:53 GMT so

System.FormatException: 'String 'Sat, 06 Jun 2021 17:03:53 GMT' was not recognized as a valid DateTime because the day of week

=> certainly need to change the library to take it into account or do a try/catch for this kind of error and return that the license is invalid.

dragonfly22000 commented 4 years ago

@sylvainonweb Thanks for the answer ,effectively, i'm going to put a Try catch and make a pop-up in case of exception. Thank you again.