Пример #1
0
 /// <summary>
 /// This is responsible for saving the license into the proper locations.
 /// </summary>
 /// <param name="type">The object <c>Type</c> being licensed.</param>
 /// <param name="license">The license to save.</param>
 /// <param name="key">The encryption key to verify the license.</param>
 private void Save(Type type, LicenseFile license, RSACryptoServiceProvider key)
 {
     Debug.WriteLine("OpenLicenseProvider: PerformSave function");
     if (CheckIfWebApplication()) // Web app
     {
         //Force the save outside the bin directory since this causes the app to restart.
         string absoluteFilePath = HttpContext.Current.Server.MapPath("~" + Path.DirectorySeparatorChar) + license.Product.LicFileName;
         Save(license, key, false, absoluteFilePath);
         WebApplicationLicenseCollector.AddLicense2Collector(type, license);
     }
     else
     {
         Save(license, key, true, FileNames.ApplicationDataPath);
     }
 }
Пример #2
0
        private LicenseFile GetLicense(Type pType, RSACryptoServiceProvider key)
        {
            StringBuilder  AnyErrorInformation = new StringBuilder();
            LicenseFile    currentLicense      = null;
            List <License> licenses            = new List <License>();
            string         licFile             = FileNames.LicenseFileName;

            // Look for the license in the cache otherwise get it from AppData or Isolated Storage
            try
            {
                if (CheckIfWebApplication())
                {
                    WebApplicationLicenseCollector.AddLicenseFromCollector(pType, licenses);
                }
            }
            catch (Exception ex)
            {
                AnyErrorInformation.Append(Resources.ErrStr_GetLicensee_UnableToGetLicenseFrom_web);
                AnyErrorInformation.AppendLine(ex.Message);
            }
            try
            {
                AddLicenseFromIsolatedStorage(licFile, key, licenses);
            }
            catch (Exception ex)
            {
                AnyErrorInformation.Append(Resources.ErrStr_GetLicensee_UnableToGetLicenseFrom_isolatedstorage);
                AnyErrorInformation.AppendLine(ex.Message);
            }
            try
            {
                AddLicenseFromPaths(key, licenses);
            }
            catch (Exception ex)
            {
                AnyErrorInformation.Append(Resources.ErrStr_GetLicensee_UnableToGetLicenseFrom_default);
                AnyErrorInformation.AppendLine(ex.Message);
            }
            bool requiredToGetNewestLicense = false;

            //Compare currentLicense with Alternate License
            if (licenses.Count > 0)
            {
                foreach (LicenseFile item in licenses)
                {
                    if (currentLicense == null)
                    {
                        currentLicense = item;
                    }
                    else
                    if (currentLicense.LicenseUID != item.LicenseUID ||
                        currentLicense.LicModificationUID != item.LicModificationUID)
                    {
                        requiredToGetNewestLicense = true;
                        break;
                    }
                    else if (currentLicense.Statistics.HitCount < item.Statistics.HitCount)
                    {
                        //Replace the current license since the alternate is older (in the mean of hit count).
                        currentLicense = item;
                    }
                }
                if (requiredToGetNewestLicense)
                {
                    foreach (LicenseFile item in licenses)
                    {
                        if (currentLicense == null || currentLicense.ModificationDate < item.ModificationDate)
                        {
                            //Replace the current license since the alternate is older.
                            currentLicense = item;
                        }
                    }
                }
            }
            else
            {
                throw new LicenseFileException(AnyErrorInformation.ToString());
            }
            return(currentLicense);
        }