protected void AddButton_OnClick(object sender, EventArgs e)
        {
            ParsedLicense parsedLicense = ParsedLicense.Deserialize(this.licenseKeyTextBox.Text);

            if (parsedLicense == null)
            {
                this.errorLabel.Text    = "Invalid license key.";
                this.errorLabel.Visible = true;
                return;
            }

            if (!parsedLicense.IsLicenseServerEligible())
            {
                this.errorLabel.Text = string.Format("Cannot add a {0} of {1} to the server.",
                                                     parsedLicense.GetLicenseTypeName() ?? "(unknown license type)",
                                                     parsedLicense.GetProductName( ));
                this.errorLabel.Visible = true;
                return;
            }

            License license = new License
            {
                LicenseId   = parsedLicense.LicenseId,
                LicenseKey  = ParsedLicense.CleanLicenseString(this.licenseKeyTextBox.Text),
                CreatedOn   = VirtualDateTime.UtcNow,
                ProductCode = parsedLicense.Product.ToString(),
            };

            Database db = new Database();

            if (db.Licenses.Any(l => l.LicenseId == license.LicenseId))
            {
                this.errorLabel.Text    = "The given license has been added already.";
                this.errorLabel.Visible = true;
                return;
            }

            db.Licenses.InsertOnSubmit(license);
            db.SubmitChanges();

            this.Response.Redirect("..");
        }
        private static LicenseState GetLicenseState(Database db, License license, Version version, DateTime?buildDate, DateTime now, Dictionary <int, LicenseState> cache, Dictionary <int, string> errors)
        {
            LicenseState licenseState;

            if (cache.TryGetValue(license.LicenseId, out licenseState))
            {
                return(licenseState);
            }

            ParsedLicense parsedLicense = ParsedLicenseManager.GetParsedLicense(license.LicenseKey);

            if (parsedLicense == null)
            {
                errors[license.LicenseId] = string.Format("The license key #{0} is invalid.", license.LicenseId);
                return(null);
            }

            if (parsedLicense.MinPostSharpVersion > ApplicationInfo.Version)
            {
                errors[license.LicenseId] = string.Format(
                    "The license #{0} requires higher version of PostSharp on the License Server. Please upgrade PostSharp NuGet package of the License Server to >= {1}.{2}.{3}",
                    license.LicenseId,
                    parsedLicense.MinPostSharpVersion.Major,
                    parsedLicense.MinPostSharpVersion.Minor,
                    parsedLicense.MinPostSharpVersion.Build);
                return(null);
            }

            if (parsedLicense.MinPostSharpVersion > version)
            {
                errors[license.LicenseId] = string.Format(
                    "The license #{0} of type {1} requires PostSharp version >= {2}.{3}.{4} but the requested version is {5}.{6}.{7}.",
                    license.LicenseId,
                    parsedLicense.LicenseType,
                    parsedLicense.MinPostSharpVersion.Major,
                    parsedLicense.MinPostSharpVersion.Minor,
                    parsedLicense.MinPostSharpVersion.Build,
                    version.Major,
                    version.Minor,
                    version.Build);
                return(null);
            }

            if (!parsedLicense.IsLicenseServerEligible())
            {
                errors[license.LicenseId] = string.Format("The license #{0}, of type {1}, cannot be used in the license server.",
                                                          license.LicenseId, parsedLicense.LicenseType);
                return(null);
            }


            if (!(buildDate == null || parsedLicense.SubscriptionEndDate == null || buildDate <= parsedLicense.SubscriptionEndDate))
            {
                // PostSharp version number has been introduced in the license server protocol in PostSharp v5.
                if (version.Major >= 5)
                {
                    errors[license.LicenseId] = string.Format(
                        "The maintenance subscription of license #{0} ends on {1:d} but the requested version {2}.{3}.{4} has been built on {5:d}.",
                        license.LicenseId,
                        parsedLicense.SubscriptionEndDate,
                        version.Major,
                        version.Minor,
                        version.Build,
                        buildDate);
                }
                else
                {
                    errors[license.LicenseId] = string.Format(
                        "The maintenance subscription of license #{0} ends on {1:d} but the requested version has been built on {2:d}.",
                        license.LicenseId,
                        parsedLicense.SubscriptionEndDate,
                        buildDate);
                }

                return(null);
            }


            licenseState = new LicenseState(now, db, license, parsedLicense);
            cache.Add(license.LicenseId, licenseState);
            return(licenseState);
        }