Exemplo n.º 1
0
        private const int CHECKKEYSRANDOMMAX = 50; //To randomly check keys on velodoc.com. The higher, the less often it is checked

        /// <summary>
        /// Gets the license from wherever it is stored
        /// </summary>
        /// <param name="context"></param>
        /// <param name="type"></param>
        /// <param name="instance"></param>
        /// <param name="allowExceptions"></param>
        /// <returns></returns>
        public override License GetLicense(LicenseContext context, Type type, object instance, bool allowExceptions)
        {
            const string CACHEDLIC = "VelodocLicense";

            //Try to get the VelodocLicense from the http cache
            VelodocLicense objLicenseRet = HttpRuntime.Cache[CACHEDLIC] as VelodocLicense;

            //No need to check for context.UsageMode because users get at least a GPL License

            if (objLicenseRet == null)
            {
                string      sLicenseKey;
                string      sActivationKey;
                RegistryKey objRegistryKey = null;
                try
                {
                    //All instances on the same machine share the same license key: should not be an issue.
                    //Should match keys and names in WebInstaller.
                    objRegistryKey = Registry.LocalMachine.OpenSubKey(APPROOTKEY);
                    sLicenseKey    = (string)objRegistryKey.GetValue(LICENSEKEY);
                    sActivationKey = (string)objRegistryKey.GetValue(ACTIVATIONKEY);

                    //Validate the license key (the following lines will raise an exception
                    //if the license key is not a Guid)
                    Guid gLicenseKey    = new Guid(sLicenseKey);
                    Guid gActivationKey = new Guid(sActivationKey);
                }
                catch
                {
                    //Should anything occur, user at least gets a GPL license
                    sLicenseKey    = null;
                    sActivationKey = null;
                }
                finally
                {
                    if (objRegistryKey != null)
                    {
                        objRegistryKey.Close();
                    }
                }

                //Randomly check the activation key
                if (!String.IsNullOrEmpty(sLicenseKey))
                {
                    Random objRandom = new Random();
                    if (objRandom.Next(0, CHECKKEYSRANDOMMAX).Equals(0))
                    {
                        LicensingServiceClient objLicensingServiceClient = null;

                        try
                        {
                            //Then configure the service
                            BasicHttpBinding objHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.None);
                            objHttpBinding.ProxyAddress       = null;
                            objHttpBinding.UseDefaultWebProxy = true;
                            EndpointAddress objRemoteAddress = new EndpointAddress(LICENSINGSVC);

                            //Finally call the service
                            objLicensingServiceClient = new LicensingServiceClient(objHttpBinding, objRemoteAddress);
                            int iResult = objLicensingServiceClient.Check(sLicenseKey, sActivationKey);
                            if (iResult == 1) //Activation not found
                            {
                                this.Clear();
                                sLicenseKey    = null;
                                sActivationKey = null;
                            }
                        }
                        catch
                        {
                            //If there is an exception (for example if there is no Internet connection),
                            //do nothing. Note that our servers may also not be available for any reason.
                        }
                        finally
                        {
                            if (objLicensingServiceClient != null)
                            {
                                objLicensingServiceClient.Dispose(); //We can call Dispose thanks to LicensingServiceFix
                                objLicensingServiceClient = null;
                            }
                        }
                    }
                }

                //Create Velodoc License and add to cache
                objLicenseRet = new VelodocLicense(sLicenseKey, sActivationKey);
                HttpRuntime.Cache.Add(
                    CACHEDLIC,
                    objLicenseRet,
                    null,
                    Cache.NoAbsoluteExpiration,
                    new TimeSpan(0, Constants.SlidingExpiration, 0),
                    CacheItemPriority.Default,
                    null);
            }

            return(objLicenseRet);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Set the license key, check it and persist it
        /// </summary>
        /// <param name="licenseKey"></param>
        /// <remarks>This function is our stuff and is not really part of the LicenseProvider design but there is no better place for it.</remarks>
        public void SetLicense(string licenseKey)
        {
            string sIPAddress  = null;
            string sMacAddress = null;

            NetworkInterface[] arrNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface objNetworkInterface in arrNetworkInterfaces)
            {
                if ((objNetworkInterface.OperationalStatus == OperationalStatus.Up) &&
                    (objNetworkInterface.Speed > 0) &&
                    (objNetworkInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback) &&
                    (objNetworkInterface.NetworkInterfaceType != NetworkInterfaceType.Tunnel))
                {
                    foreach (UnicastIPAddressInformation objIPAddressInfomation in objNetworkInterface.GetIPProperties().UnicastAddresses)
                    {
                        if (!objIPAddressInfomation.IPv4Mask.Equals(System.Net.IPAddress.Any))
                        {
                            sIPAddress  = objIPAddressInfomation.Address.ToString();
                            sMacAddress = objNetworkInterface.GetPhysicalAddress().ToString();
                            break;
                        }
                    }
                    if (!String.IsNullOrEmpty(sIPAddress))
                    {
                        break;
                    }
                }
            }

            //Activate the license key using a web service, first building the contract data
            ActivationData objActivationData = new ActivationData();

            objActivationData.ActivationKey = Guid.NewGuid();
            try
            {
                objActivationData.LicenseKey = new Guid(licenseKey); //Raises a FormatException if not a Guid
            }
            catch (FormatException Ex)
            {
                throw new FormatException(String.Format(
                                              Properties.Resources.Culture,
                                              Properties.Resources.ExceptionLicenseProviderInvalidKey,
                                              licenseKey),
                                          Ex);
            }
            objActivationData.MachineName    = Environment.MachineName;
            objActivationData.DomainName     = Environment.UserDomainName;
            objActivationData.UserName       = Environment.UserName;
            objActivationData.OSVersion      = Environment.OSVersion.VersionString;
            objActivationData.ProcessorCount = Environment.ProcessorCount;
            objActivationData.FmkVersion     = Environment.Version.ToString();
            objActivationData.IPAddress      = sIPAddress; //<-- See above
            objActivationData.MACAddress     = sMacAddress;
            objActivationData.ProductCode    = PRODUCTCODE;
            objActivationData.CultureName    = System.Globalization.CultureInfo.CurrentUICulture.Name;

            //Then configure the service
            BasicHttpBinding objHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.None);

            objHttpBinding.ProxyAddress       = null;
            objHttpBinding.UseDefaultWebProxy = true;
            EndpointAddress objRemoteAddress = new EndpointAddress(LICENSINGSVC);

            //Finally call the service
            LicensingServiceClient objLicensingServiceClient = null;
            int iResult = -1;

            try
            {
                objLicensingServiceClient = new LicensingServiceClient(objHttpBinding, objRemoteAddress);
                iResult = objLicensingServiceClient.Activate(objActivationData);
            }
            finally
            {
                if (objLicensingServiceClient != null)
                {
                    objLicensingServiceClient.Dispose(); //We can call Dispose thanks to LicensingServiceFix
                    objLicensingServiceClient = null;
                }
            }

            if (iResult == 0)
            {
                //All instances on the same machine share the same license key: should not be an issue.
                RegistryKey objRegistryKey = Registry.LocalMachine.CreateSubKey(APPROOTKEY);
                objRegistryKey.SetValue(LICENSEKEY, objActivationData.LicenseKey.ToString());
                objRegistryKey.SetValue(ACTIVATIONKEY, objActivationData.ActivationKey.ToString());
                objRegistryKey.Close();
            }
            else if (iResult == 1)
            {
                throw new LicenseException(this.GetType(), this,
                                           String.Format(Properties.Resources.Culture,
                                                         Properties.Resources.ExceptionLicenseProviderInvalidKey,
                                                         licenseKey));
            }
            else
            {
                throw new LicenseException(this.GetType(), this,
                                           Properties.Resources.ExceptionLicenseProviderServiceError);
            }
        }