Beispiel #1
0
        //internal License GetLicense(LicenseContext context, Type type, object instance, bool allowExceptions)
        internal static License GetLicense(Type type, string key)
        {
            //if (System.Security.SecurityManager.SecurityEnabled == false)
            //	return null;

            KrystalwareRuntimeLicense l = null;

            string assembly    = type.Assembly.GetName().Name;
            string licenseFile = assembly + ".xml.lic";

            // First, check if someone has set the license manually
            PropertyInfo[] licenseProperties = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Static);

            foreach (PropertyInfo pi in licenseProperties)
            {
                if (pi.PropertyType == typeof(KrystalwareRuntimeLicense))
                {
                    l = pi.GetValue(null, null) as KrystalwareRuntimeLicense;

                    break;
                }
            }

            // First, check to see if there's a license file with the app
            if (l == null)
            {
                l = LoadLicenseFromFile(type, HostingEnvironment.MapPath("~/"));
            }

            // Next, check to see if there's a license file in the bin folder
            if (l == null)
            {
                l = LoadLicenseFromFile(type, HostingEnvironment.MapPath("~/bin/"));
            }

            // Next, check to see if there's a license file in the App_Data folder
            if (l == null)
            {
                l = LoadLicenseFromFile(type, HostingEnvironment.MapPath("~/App_Data/"));
            }

            // Next, check to see if there's a license resource somewhere
            if (l == null)
            {
                l = LoadLicenseFromResource(type);
            }

            // Next, check to see if there's a license file with the assembly
            if (l == null)
            {
                l = LoadLicenseFromFile(type);
            }

            if (l != null)
            {
                //string key = ((LicensePublicKeyAttribute)Attribute.GetCustomAttribute(type.Assembly, typeof(LicensePublicKeyAttribute))).XmlString;

                if (key != null)
                {
                    KeyInfo keyInfo = LicenseValidator.GetKeyInfoFromXml(key);

                    LicenseValidator validator = new LicenseValidator(l);

                    if (!validator.IsValid(keyInfo))
                    {
                        l = null;
                    }
                }
                else
                {
                    l = null;
                }
            }

            // If not, create a new evaluation license
            if (l == null)
            {
                string version = type.Assembly.GetName().Version.Major.ToString();

                l = new KrystalwareRuntimeLicense(assembly, version);
            }

            return(l);
        }