Example #1
0
        public void DoLicenseCheck()
        {
            LicenseValidationResult result = Validator.CheckLicense();

            Result = result;

            /*if (result.State == LicenseState.Invalid)
             * {
             *  if (result.Issues.Contains(LicenseIssue.NoLicenseInfo))
             *  {
             *      //inform user there is no license info
             *  }
             *  else
             *  {
             *      if (result.Issues.Contains(LicenseIssue.ExpiredDateSoft))
             *      {
             *          //inform user that their license has expired but
             *          //that they may continue using the software for a period
             *      }
             *      if (result.Issues.Contains(LicenseIssue.ExpiredDateHard))
             *      {
             *          //inform user that their license has expired
             *      }
             *      if (result.Issues.Contains(LicenseIssue.ExpiredVersion))
             *      {
             *          //inform user that their license is for an earlier version
             *      }
             *      //other messages
             *  }
             *
             *  //prompt user for trial or to insert license info then decide what to do
             *  //activate trial
             *  result = Validator.ActivateTrial(45);
             *  //or save license
             *  string userLicense = "Get the license string from your user";
             *  result = Validator.CheckLicense(userLicense);
             *  //decide if you want to save the license...
             *  Validator.SaveLicense(userLicense);
             * }
             * if (result.State == LicenseState.Trial)
             * {
             *  //activate trial features
             * }
             * if (result.State == LicenseState.Valid)
             * {
             *  //activate product
             *  if (Validator.IsEdition("Standard"))
             *  {
             *      //activate pro features...
             *  }
             * }*/
        }
Example #2
0
        /// <summary>
        /// Validates the license on the server.
        /// </summary>
        /// <param name="license">The license.</param>
        /// <param name="serverUrl">The server URL.</param>
        /// <param name="assembly">The assembly to get the information from. If <c>null</c>, the entry assembly will be used.</param>
        /// <returns><c>true</c> if the license is valid, <c>false</c> otherwise.</returns>
        public LicenseValidationResult ValidateLicenseOnServer(string license, string serverUrl, Assembly assembly = null)
        {
            Argument.IsNotNullOrWhitespace(() => license);
            Argument.IsNotNullOrWhitespace(() => serverUrl);

            if (assembly == null)
            {
                assembly = AssemblyHelper.GetEntryAssembly();
            }

            LicenseValidationResult validationResult = null;

            try
            {
                var webRequest = WebRequest.Create(serverUrl);
                webRequest.ContentType = "application/json";
                webRequest.Method      = "POST";

                using (var sw = new StreamWriter(webRequest.GetRequestStream()))
                {
                    var version = "unknown version";
                    if (assembly != null)
                    {
                        try
                        {
                            version = assembly.InformationalVersion();
                            if (string.IsNullOrWhiteSpace(version))
                            {
                                version = assembly.Version();
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex, "Failed to retrieve the product version");
                        }
                    }

                    var serviceLicenseValidation = new ServerLicenseValidation
                    {
                        MachineId      = _identificationService.GetMachineId(),
                        ProductName    = (assembly != null) ? assembly.Product() : "unknown product",
                        ProductVersion = version,
                        License        = license
                    };

                    var json = JsonConvert.SerializeObject(serviceLicenseValidation);

                    sw.Write(json);
                }

                using (var httpWebResponse = webRequest.GetResponse())
                {
                    using (var responseStream = httpWebResponse.GetResponseStream())
                    {
                        using (var streamReader = new StreamReader(responseStream))
                        {
                            var json = streamReader.ReadToEnd();
                            validationResult = JsonConvert.DeserializeObject <LicenseValidationResult>(json);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to validate the license on the server");
            }

            if (validationResult == null)
            {
                validationResult = new LicenseValidationResult()
                {
                    // We return success if we can't validate on the server, then it's up to the client to validate
                    IsValid        = true,
                    AdditionalInfo = "Failed to check the license on the server"
                };
            }

            return(validationResult);
        }
        /// <summary>
        /// Validates the license on the server.
        /// </summary>
        /// <param name="license">The license.</param>
        /// <param name="serverUrl">The server URL.</param>
        /// <param name="assembly">The assembly to get the information from. If <c>null</c>, the entry assembly will be used.</param>
        /// <returns><c>true</c> if the license is valid, <c>false</c> otherwise.</returns>
        public LicenseValidationResult ValidateLicenseOnServer(string license, string serverUrl, Assembly assembly = null)
        {
            Argument.IsNotNullOrWhitespace(() => license);
            Argument.IsNotNullOrWhitespace(() => serverUrl);

            if (assembly == null)
            {
                assembly = AssemblyHelper.GetEntryAssembly();
            }

            LicenseValidationResult validationResult = null;

            try
            {
                var webRequest = WebRequest.Create(serverUrl);
                webRequest.ContentType = "application/json";
                webRequest.Method = "POST";

                using (var sw = new StreamWriter(webRequest.GetRequestStream()))
                {
                    var version = "unknown version";
                    if (assembly != null)
                    {
                        try
                        {
                            version = assembly.InformationalVersion();
                            if (string.IsNullOrWhiteSpace(version))
                            {
                                version = assembly.Version();
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex, "Failed to retrieve the product version");
                        }
                    }

                    var serviceLicenseValidation = new ServerLicenseValidation
                    {
                        MachineId = _identificationService.GetMachineId(),
                        ProductName = (assembly != null) ? assembly.Product() : "unknown product",
                        ProductVersion = version,
                        License = license
                    };

                    var json = JsonConvert.SerializeObject(serviceLicenseValidation);

                    sw.Write(json);
                }

                using (var httpWebResponse = webRequest.GetResponse())
                {
                    using (var responseStream = httpWebResponse.GetResponseStream())
                    {
                        using (var streamReader = new StreamReader(responseStream))
                        {
                            var json = streamReader.ReadToEnd();
                            validationResult = JsonConvert.DeserializeObject<LicenseValidationResult>(json);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to validate the license on the server");
            }

            if (validationResult == null)
            {
                validationResult = new LicenseValidationResult()
                {
                    // We return success if we can't validate on the server, then it's up to the client to validate
                    IsValid = true,
                    AdditionalInfo = "Failed to check the license on the server"
                };
            }

            return validationResult;
        }