Ejemplo n.º 1
0
        /// <summary>
        /// The validate license.
        /// </summary>
        /// <param name="fullSlowCheck">The full slow check.</param>
        /// <returns>
        /// The <see cref="LicenseState" />.
        /// </returns>
        public LicenseState ValidateLicense(bool fullSlowCheck, bool withCheckConnection = true)
        {
            if (this._foundLicenseState != null)
            {
                return(this._foundLicenseState);
            }

            this._foundLicenseState = new LicenseState
            {
                Instance = this.Instance
            };

            if (this.LicenseInfo == null)
            {
                if (AppVersionHelper.IsNotRelease())
                {
                    return(this._foundLicenseState);
                }

                this._foundLicenseState.AddProblem(LicenseProblemType.LicenseNotDefined, string.Empty);
            }
            else
            {
                this._foundLicenseState.AddProblems(this.LicenseInfo.IsLicenseInfoCorrect());

                DateTime dateTime;

                if (fullSlowCheck && withCheckConnection)
                {
                    if (this._serverProperties != null)
                    {
                        dateTime = this._serverProperties.Date;
                    }
                    else
                    {
                        try
                        {
                            ServerProperties props = ServerProperties.Query(this);

                            dateTime = props.Date;

                            this._serverProperties = props;
                        }
                        catch (Exception exc)
                        {
                            log.Error("Exception:", exc);

                            log.ErrorFormat(
                                "Instance:'{0}';Authentication:'{1}';Exception:'{2}'",
                                Instance,
                                Authentication,
                                exc
                                );

                            dateTime = DateTime.MaxValue;
                        }
                    }
                }
                else
                {
                    dateTime = DateTime.Now;
                }

                var buildDateObject =
                    Assembly.GetExecutingAssembly()
                    .GetCustomAttributes(typeof(BuildDateAttribute), false)
                    .FirstOrDefault();

                DateTime buildDate = DateTime.MaxValue;

                if (buildDateObject is BuildDateAttribute)
                {
                    buildDate = (buildDateObject as BuildDateAttribute).BuildDate;
                }

                if (dateTime == DateTime.MaxValue)
                {
                    this._foundLicenseState.AddProblem(LicenseProblemType.CantConnect, string.Empty);
                }
                else if (this._foundLicenseState.IsCorrect && fullSlowCheck && (dateTime > this.LicenseInfo.ExpiryDate))
                {
                    this._foundLicenseState.AddProblem(LicenseProblemType.Expired, string.Empty);
                }
                else if (buildDate > this.LicenseInfo.BuildExpiryDate)
                {
                    this._foundLicenseState.AddProblem(LicenseProblemType.BuildExpiryDateNotValid, string.Empty);
                }
                else
                {
                    if (AppVersionHelper.IsNotDebug())
                    {
                        string hash = this.GetHash();

                        CryptoProcessor cryptoProcessor =
                            new CryptoProcessor(
                                Program.Model.Settings.SystemSettings.PublicKeyXmlSign,
                                Program.Model.Settings.SystemSettings.PrivateKeyXmlDecrypt
                                );

                        if (!cryptoProcessor.Verify(hash, this.LicenseInfo.Signature))
                        {
                            this._foundLicenseState.AddProblem(LicenseProblemType.WrongSignature, string.Empty);
                        }
                    }
                }
            }

            var result = this._foundLicenseState;

            if (!fullSlowCheck)
            {
                this._foundLicenseState = null;
            }

            return(result);
        }