Exemple #1
0
        /// <summary>
        /// Activates the license by contacting the Cryptlex servers. It
        /// validates the key and returns with encrypted and digitally signed token
        /// which it stores and uses to activate your application.
        ///
        /// This function should be executed at the time of registration, ideally on
        /// a button click.
        /// </summary>
        /// <returns>LA_OK, LA_EXPIRED, LA_SUSPENDED, LA_FAIL</returns>
        public int ActivateLicense()
        {
            if (String.IsNullOrEmpty(this._productId))
            {
                throw new LexActivatorException(LexStatusCodes.LA_E_PRODUCT_ID);
            }
            if (String.IsNullOrEmpty(this._rsaPublicKey))
            {
                throw new LexActivatorException(LexStatusCodes.LA_E_RSA_PUBLIC_KEY);
            }

            _licenseKey = LexDataStore.GetValue(this._productId, LexConstants.KEY_LICENSE_KEY);
            if (String.IsNullOrEmpty(_licenseKey))
            {
                throw new LexActivatorException(LexStatusCodes.LA_E_LICENSE_KEY);
            }

            _activationPayload = new ActivationPayload();
            var meterAttributes = new List <ActivationMeterAttribute>();
            int status          = LexActivationService.ActivateFromServer(_productId, _licenseKey, _rsaPublicKey, _activationPayload, meterAttributes);

            if (LexValidator.ValidateSuccessCode(status))
            {
                StartTimer(_activationPayload.ServerSyncInterval, _activationPayload.ServerSyncInterval);
                return(status);
            }
            throw new LexActivatorException(status);
        }
Exemple #2
0
        /// <summary>
        /// Deactivates the license activation and frees up the corresponding activation
        /// slot by contacting the Cryptlex servers.
        ///
        /// This function should be executed at the time of de-registration, ideally on
        /// a button click.
        /// </summary>
        /// <returns>LA_OK</returns>
        public int DeactivateLicense()
        {
            int status = IsLicenseValid();

            if (LexValidator.ValidateSuccessCode(status))
            {
                status = LexActivationService.DeactivateFromServer(_productId, _activationPayload);
                if (status == LexStatusCodes.LA_OK)
                {
                    return(status);
                }
            }
            throw new LexActivatorException(status);
        }
Exemple #3
0
        /// <summary>
        /// Gets the metadata associated with the license user.
        /// </summary>
        /// <param name="key">key to retrieve the value</param>
        /// <returns>Returns the value of metadata for the key.</returns>
        public string GetLicenseUserMetadata(string key)
        {
            int status = IsLicenseValid();

            if (LexValidator.ValidateSuccessCode(status))
            {
                string value = LexActivationService.GetMetadata(key, _activationPayload.UserMetadata);
                if (value == null)
                {
                    throw new LexActivatorException(LexStatusCodes.LA_E_METADATA_KEY_NOT_FOUND);
                }
                return(value);
            }
            throw new LexActivatorException(status);
        }
Exemple #4
0
        /// <summary>
        /// Gets the license meter attribute allowed uses and total uses.
        /// </summary>
        /// <param name="name">name of the meter attribute</param>
        /// <returns>Returns the values of meter attribute allowed and total uses.</returns>
        public LicenseMeterAttribute GetLicenseMeterAttribute(string name)
        {
            int status = IsLicenseValid();

            if (LexValidator.ValidateSuccessCode(status))
            {
                var licenseMeterAttribute = LexActivationService.GetLicenseMeterAttribute(name, _activationPayload.LicenseMeterAttributes);
                if (licenseMeterAttribute == null)
                {
                    throw new LexActivatorException(LexStatusCodes.LA_E_METER_ATTRIBUTE_NOT_FOUND);
                }
                return(licenseMeterAttribute);
            }
            throw new LexActivatorException(status);
        }
Exemple #5
0
        private void LicenseTimerCallback(Object stateInfo)
        {
            if (_activationPayload.IsValid == false)   // invalid as license was dropped
            {
                StopTimer();
                return;
            }
            var meterAttributes = new List <ActivationMeterAttribute>();
            int status          = LexActivationService.ActivateFromServer(_productId, _licenseKey, _rsaPublicKey, _activationPayload, meterAttributes, true);

            if (!LexValidator.ValidateServerSyncAllowedStatusCodes(status))
            {
                StopTimer();
                this._callback(status);
                return;
            }
            this._callback(status);
        }
Exemple #6
0
        /// <summary>
        /// Gets the meter attribute uses consumed by the activation.
        /// </summary>
        /// <param name="name"></param>
        /// <returns>Returns the value of meter attribute uses by the activation.</returns>
        public long GetActivationMeterAttributeUses(string name)
        {
            int status = IsLicenseValid();

            if (LexValidator.ValidateSuccessCode(status))
            {
                bool exists = false;
                exists = LexActivationService.MeterAttributeExists(name, _activationPayload.LicenseMeterAttributes);
                if (!exists)
                {
                    throw new LexActivatorException(LexStatusCodes.LA_E_METER_ATTRIBUTE_NOT_FOUND);
                }
                var activationMeterAttribute = LexActivationService.GetActivationMeterAttribute(name, _activationPayload.ActivationMeterAttributes);
                if (activationMeterAttribute == null)
                {
                    return(0);
                }
                return(activationMeterAttribute.Uses);
            }
            throw new LexActivatorException(status);
        }
Exemple #7
0
        private int UpdateMeterAttributeUses(string name, List <ActivationMeterAttribute> meterAttributes, long uses)
        {
            string normalizedName = name.ToUpper();
            bool   exists         = false;

            foreach (var item in meterAttributes)
            {
                if (normalizedName == item.Name.ToUpper())
                {
                    item.Uses = uses;;
                    exists    = true;
                    break;
                }
            }
            if (!exists)
            {
                meterAttributes.Add(new ActivationMeterAttribute(name, uses));
            }

            int status = LexActivationService.ActivateFromServer(_productId, _licenseKey, _rsaPublicKey, _activationPayload, meterAttributes, true);

            return(status);
        }