Ejemplo n.º 1
0
        private static void ValidateFreeLicenseKey(string licenseText)
        {
            if (!IsFreeLicenseKey(licenseText))
            {
                throw new NotSupportedException("Not a free License Key");
            }

            var envKey = Environment.GetEnvironmentVariable("SERVICESTACK_LICENSE");

            if (envKey == licenseText)
            {
                throw new LicenseException("Cannot use SERVICESTACK_LICENSE Environment variable with free License Keys, " +
                                           "please use Licensing.RegisterLicense() in source code.");
            }

            LicenseKey key = null;

            if (licenseText.StartsWith(IndividualPrefix))
            {
                key = VerifyIndividualLicense(licenseText);
                if (key == null)
                {
                    throw new LicenseException("Individual License Key is invalid.");
                }
            }
            else if (licenseText.StartsWith(OpenSourcePrefix))
            {
                key = VerifyOpenSourceLicense(licenseText);
                if (key == null)
                {
                    throw new LicenseException("Open Source License Key is invalid.");
                }
            }
            else
            {
                throw new NotSupportedException("Not a free License Key");
            }

            var releaseDate = Env.GetReleaseDate();

            if (releaseDate > key.Expiry)
            {
                throw new LicenseException($"This license has expired on {key.Expiry:d} and is not valid for use with this release.\n"
                                           + "Check https://servicestack.net/free for eligible renewals.").Trace();
            }

            __activatedLicense = new __ActivatedLicense(key);
        }
Ejemplo n.º 2
0
        private static void ValidateLicenseKey(LicenseKey key)
        {
            var releaseDate = Env.GetReleaseDate();

            if (releaseDate > key.Expiry)
            {
                throw new LicenseException($"This license has expired on {key.Expiry:d} and is not valid for use with this release."
                                           + ContactDetails).Trace();
            }

            if (key.Type == LicenseType.Trial && DateTime.UtcNow > key.Expiry)
            {
                throw new LicenseException($"This trial license has expired on {key.Expiry:d}." + ContactDetails).Trace();
            }

            __activatedLicense = new __ActivatedLicense(key);
        }
Ejemplo n.º 3
0
 public static void RemoveLicense()
 {
     __activatedLicense = null;
 }
        public static void RegisterLicense(string licenseKeyText)
        {
            JsConfig.InitStatics();

            if (__activatedLicense != null) //Skip multiple license registrations. Use RemoveLicense() to reset.
            {
                return;
            }

            string subId = null;
            var    hold  = Thread.CurrentThread.CurrentCulture;

            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            try
            {
                var parts = licenseKeyText.SplitOnFirst('-');
                subId = parts[0];

                if (int.TryParse(subId, out var subIdInt) && revokedSubs.Contains(subIdInt))
                {
                    throw new LicenseException("This subscription has been revoked. " + ContactDetails);
                }

                var key = VerifyLicenseKeyText(licenseKeyText);
                ValidateLicenseKey(key);
            }
            catch (PlatformNotSupportedException)
            {
                // Allow usage in environments like dotnet script
                __activatedLicense = new __ActivatedLicense(new LicenseKey {
                    Type = LicenseType.Indie
                });
            }
            catch (Exception ex)
            {
                //bubble unrelated project Exceptions
                if (ex is FileNotFoundException || ex is FileLoadException || ex is BadImageFormatException)
                {
                    throw;
                }

                if (ex is LicenseException)
                {
                    throw;
                }

                var msg = "This license is invalid." + ContactDetails;
                if (!string.IsNullOrEmpty(subId))
                {
                    msg += $" The id for this license is '{subId}'";
                }

                lock (typeof(LicenseUtils))
                {
                    try
                    {
                        var key = PclExport.Instance.VerifyLicenseKeyTextFallback(licenseKeyText);
                        ValidateLicenseKey(key);
                    }
                    catch (Exception exFallback)
                    {
                        if (exFallback is FileNotFoundException || exFallback is FileLoadException || exFallback is BadImageFormatException)
                        {
                            throw;
                        }

                        throw new LicenseException(msg, exFallback).Trace();
                    }
                }

                throw new LicenseException(msg, ex).Trace();
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = hold;
            }
        }