Ejemplo n.º 1
0
 public void SetLicense(Type type, LicFileLicense license)
 {
     SetSavedLicenseKey(type, license.LicenseKey);
     licenses[type] = license;
 }
Ejemplo n.º 2
0
        //protected abstract string LicenseName
        //{
        //    get;
        //}


        public override License GetLicense(LicenseContext c, Type type, object instance, bool allowExceptions)
        {
            ALinqLicenseContext context;

            if (c is ALinqLicenseContext)
            {
                context = (ALinqLicenseContext)c;
            }
            else
            {
                context = new ALinqLicenseContext();
            }

            LicenseManager.CurrentContext = context;
            LicFileLicense license = context.GetSavedLicense(type);

            if (license == null)
            {
                #region Read license from Attribute
                if (instance is DataContext)
                {
                    var attributes = instance.GetType().GetCustomAttributes(true)
                                     .Where(o => o is LicenseAttribute).Cast <LicenseAttribute>();
                    var count = attributes.Count();
                    if (count == 1)
                    {
                        var attribute = attributes.Single();
                        license = new LicFileLicense(this, attribute.UserName, attribute.Key, null);
                    }
                }
                #endregion

                #region Read license from alinq.lic
                if (license == null)
                {
                    Assembly assembly = null;
                    if (instance is DataContext && instance.GetType() != typeof(DataContext))
                    {
                        assembly = instance.GetType().Assembly;
                    }

                    Assembly licenseFileAssembly;
                    var      stream = SearchLicenseStream(assembly, out licenseFileAssembly);
                    if (stream != null)
                    {
                        string      userName;
                        string      key;
                        string      assemblyName;
                        CultureInfo culture;
                        ParseLicense(type, instance, stream, out userName, out key, out assemblyName, out culture);
                        stream.Dispose();

                        //==============================================================================
                        // 说明:key 允许为空,当为 Free 版本时
                        // if (key == null)
                        //     throw Error.LicenseFail(type, instance, string.Format("The {0} license key could not found in the ALinq.lic file.", type));
                        //==============================================================================

                        if (assemblyName != licenseFileAssembly.GetName().Name)
                        {
                            throw Error.AssemblyNameNotMatch(type, instance, assemblyName, licenseFileAssembly.GetName().Name);
                        }

                        license = new LicFileLicense(this, userName, key, culture);
                    }
                }
                #endregion

                #region Read license from registry.
                //if (license == null)
                //{
                //    var platform = Environment.OSVersion.Platform;
                //    if (platform == PlatformID.Win32NT || platform == PlatformID.Win32S ||
                //        platform == PlatformID.Win32Windows || platform == PlatformID.Win32Windows)
                //    {
                //        try
                //        {
                //            var regALinq = Registry.ClassesRoot.OpenSubKey("SOFTWARE\\ALinq", true);
                //            if (regALinq != null)
                //            {
                //                var obj = regALinq.GetValue("PreLoad");
                //                if (obj == null)
                //                {
                //                    obj = DateTime.Now.ToString("yyyy-MM-dd");
                //                    var bs = Encoding.ASCII.GetBytes((string)obj);
                //                    obj = Convert.ToBase64String(bs);
                //                    regALinq.SetValue("PreLoad", obj);
                //                }

                //                var preLoad = (string)obj;
                //                var bytes = Convert.FromBase64String(preLoad);
                //                var strDate = Encoding.ASCII.GetString(bytes);
                //                var date = DateTime.Parse(strDate);
                //                if (date > DateTime.Now)
                //                    throw Error.LicenseFail(type, instance, "This software is expired.");
                //                else
                //                {
                //                    var data = Encoding.ASCII.GetBytes(DateTime.Now.ToString());
                //                    var str = Convert.ToBase64String(data);
                //                    regALinq.SetValue("PreLoad", str);
                //                }

                //                obj = regALinq.GetValue("Date");
                //                if (obj == null)
                //                {
                //                    obj = DateTime.Now.ToString("yyyy-MM-dd");
                //                    var bs = Encoding.ASCII.GetBytes((string)obj);
                //                    obj = Convert.ToBase64String(bs);
                //                    regALinq.SetValue("Date", obj);
                //                }

                //                var encryptDate = (string)obj;
                //                bytes = Convert.FromBase64String(encryptDate);
                //                strDate = Encoding.ASCII.GetString(bytes);
                //                date = DateTime.Parse(strDate);

                //                var days = DAYS - (DateTime.Now - date).Days;
                //                license = new LicFileLicense(this, Constants.TrialUserName, date.ToString(), null) { ExpiredDays = days };
                //            }
                //        }
                //        catch (Exception exc)
                //        {
                //            if (exc is LicenseException)
                //                throw;
                //        }
                //    }
                //}
                #endregion

                if (license != null)
                {
                    //licenses[type] = license;
                    context.SetLicense(type, license);//.SetSavedLicenseKey(type, license.LicenseKey);
                }
            }

            if (license == null)
            {
                throw new LicenseException(type, instance, string.Format("Found license fail!"));
            }

            //验证授权
            ValidateLicense(type, instance, license);
            return(license);
        }