コード例 #1
0
        /// <summary>Validates a volume license.</summary>
        /// <returns>Returns true if successful.  If false is returned, check the <see cref="License.LastError"/> property for details.</returns>
        internal bool ValidateVolumeLicense()
        {
            VolumeLicense vlic = new VolumeLicense();

            if (!vlic.LoadVolumeLicenseFile(LicenseConfiguration.VolumeLicenseFilePath))
            {
                LastError = vlic.LastError;
                return(false);
            }

            if (LicenseID != vlic.LicenseID)
            {
                LastError = new LicenseError(LicenseError.ERROR_LICENSE_SYSTEM_IDENTIFIERS_DONT_MATCH);
                return(false);
            }

            if (!vlic.Validate())
            {
                LastError = vlic.LastError;
                return(false);
            }

            //TODO: Uncomment the block of code below if you wish to have volume licenses attempt to refresh with SOLO Server.
            //      Note that this can cause the application to experience some delay while the refresh is being attempted,
            //      which is especially noticeable when SOLO Server cannot be reached.

            /*if (!string.IsNullOrEmpty(vlic.InstallationID) &&
             *  LicenseTypes.Unlicensed != vlic.TypeOfLicense &&
             *  LicenseMethods.IsRefreshLicenseAttemptDue(vlic.SignatureDate))
             * {
             *  using (XmlLicenseFileService ws = m_Settings.CreateNewXmlLicenseFileServiceObject())
             *  {
             *      //If a refresh attempt should be made, try to perform a license refresh with SOLO Server.
             *      if (!vlic.RefreshLicense(ws) &&
             *          (vlic.LastError.ErrorNumber != LicenseError.ERROR_WEBSERVICE_CALL_FAILED || LicenseMethods.IsRefreshLicenseRequired(vlic.SignatureDate)))
             *      {
             *          //The refresh failed and was required, or SOLO Server returned an error.
             *          LastError = vlic.LastError;
             *          return false;
             *      }
             *  }
             * }*/

            //Create a list of validations to perform.
            List <SystemValidation> validations = new List <SystemValidation>();

            //Add a validation to make sure there is no active system clock tampering taking place.
            validations.Add(new SystemClockValidation());

            //Only validate the system identifiers if we activated using trigger codes.  (Ignore this validation if it is a volume license.)
            if (ProductOption.OptionType == LicenseProductOption.ProductOptionType.DownloadableLicenseWithTriggerCodeValidation)
            {
                //Add a validation to make sure this system is authorized to use the activated license.
                validations.Add(new SystemIdentifierValidation(
                                    AuthorizedIdentifiers,
                                    CurrentIdentifiers,
                                    SystemIdentifierValidation.REQUIRE_EXACT_MATCH));
            }

            //Run all of the validations (in the order they were added), and make sure all of them succeed.
            foreach (SystemValidation validation in validations)
            {
                if (!validation.Validate())
                {
                    LastError = validation.LastError;
                    return(false);
                }
            }

            return(true);
        }
コード例 #2
0
        /// <summary>Initializes the volume license</summary>
        /// <returns>Returns true if successful.  If false is returned, check the <see cref="License.LastError"/> property for details.</returns>
        internal bool InitializeVolumeLicense()
        {
            VolumeLicense vlic = new VolumeLicense();

            //First try to load the volume license.
            if (!vlic.LoadVolumeLicenseFile(LicenseConfiguration.VolumeLicenseFilePath))
            {
                LastError = vlic.LastError;
                return(false);
            }

            if (!File.Exists(LicenseConfiguration.LicenseFilePath) ||
                !LoadFile(LicenseConfiguration.LicenseFilePath) ||
                (ProductOption.OptionType != LicenseProductOption.ProductOptionType.VolumeLicense &&
                 ProductOption.OptionType != LicenseProductOption.ProductOptionType.DownloadableLicenseWithTriggerCodeValidation) ||
                (ProductOption.OptionType != vlic.ProductOption.OptionType && (
                     ProductOption.OptionType == LicenseProductOption.ProductOptionType.VolumeLicense ||
                     ProductOption.OptionType == LicenseProductOption.ProductOptionType.DownloadableLicenseWithTriggerCodeValidation)))
            {
                //If we reach the code in this if statement, one of the following conditions have been met:
                //    * The initial self-signed/writable license file has not been created yet, and the volume/downloadable license file is present; OR
                //    * A self-signed/writable license file has been created, but for some other kind of license (i.e. an evaluation); OR
                //    * A self-signed/writable license file has been created, but the volume license was swapped out with a downloadable license or vice versa.
                //
                //   Under these conditions, we want to update the writable copy of the license file with the content from the new, volume/downloadable license file.

                //Now try to load the volume license using the self-signed/writable license object.
                if (!LoadFile(LicenseConfiguration.VolumeLicenseFilePath))
                {
                    return(false);
                }

                //Now save the self-signed/writable license file with the volume license's data.
                if (!SaveLicenseFile())
                {
                    return(false);
                }
            }
            else if ((LicenseConfiguration.DownloadableLicenseOverwriteWithNewerAllowed || LicenseConfiguration.DownloadableLicenseOverwriteWithOlderAllowed) &&
                     vlic.LoadVolumeLicenseFile(LicenseConfiguration.VolumeLicenseFilePath) &&
                     Validate())
            {
                bool isNewer = LicenseConfiguration.DownloadableLicenseOverwriteWithNewerAllowed && vlic.SignatureDate.Subtract(SignatureDate).TotalDays > 0;
                bool isOlder = LicenseConfiguration.DownloadableLicenseOverwriteWithOlderAllowed && vlic.SignatureDate.Subtract(SignatureDate).TotalDays < 0;
                if (isNewer || isOlder)
                {
                    //The volume/downloadable license was loaded, it is newer than the existing self-signed/writable license file, and
                    //the existing self-signed writable license file is valid.

                    //TODO: Store any data that should not be overwritten by the new, downloadable license file here.

                    //Store the date the license was validated so we can restore it after updating the license.
                    DateTime dateValidated = DateDownloadableLicenseValidated;

                    //Now load the new license data from the volume/downloadable license file.
                    if (!LoadFile(LicenseConfiguration.VolumeLicenseFilePath))
                    {
                        return(false);
                    }

                    //If activation is not required, restore the date the license was validated.
                    if ((isNewer && !LicenseConfiguration.DownloadableLicenseOverwriteWithNewerRequiresActivation) ||
                        (isOlder && !LicenseConfiguration.DownloadableLicenseOverwriteWithOlderRequiresActivation))
                    {
                        DateDownloadableLicenseValidated = dateValidated;
                    }

                    //TODO: Restore any data that should not be overwritten by the new, downloadable license file here.

                    //Save the new data.
                    if (!SaveLicenseFile())
                    {
                        return(false);
                    }

                    //Re-load the new license file one more time to make sure we have the right data.
                    if (!LoadFile(LicenseConfiguration.LicenseFilePath))
                    {
                        return(false);
                    }
                }
            }

            //Finally, return the validation result.
            return(Validate());
        }