示例#1
0
        /// <summary>
        ///    <para>Gets a license for the instance of the component and determines if it is valid.</para>
        /// </summary>
        public override License GetLicense(LicenseContext context, Type type, object instance, bool allowExceptions)
        {
            LicFileLicense lic = null;

            Debug.Assert(context != null, "No context provided!");
            if (context != null)
            {
                if (context.UsageMode == LicenseUsageMode.Runtime)
                {
                    string key = context.GetSavedLicenseKey(type, null);
                    if (key != null && IsKeyValid(key, type))
                    {
                        lic = new LicFileLicense(this, key);
                    }
                }

                if (lic == null)
                {
                    string modulePath = null;

                    if (context != null)
                    {
                        ITypeResolutionService resolver = (ITypeResolutionService)context.GetService(typeof(ITypeResolutionService));
                        if (resolver != null)
                        {
                            modulePath = resolver.GetPathOfAssembly(type.Assembly.GetName());
                        }
                    }

                    if (modulePath == null)
                    {
                        modulePath = type.Module.FullyQualifiedName;
                    }

                    string moduleDir = Path.GetDirectoryName(modulePath);
                    string licenseFile = moduleDir + "\\" + type.FullName + ".lic";

                    Debug.WriteLine($"Looking for license in: {licenseFile}");
                    if (File.Exists(licenseFile))
                    {
                        Stream licStream = new FileStream(licenseFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                        StreamReader sr = new StreamReader(licStream);
                        string s = sr.ReadLine();
                        sr.Close();
                        if (IsKeyValid(s, type))
                        {
                            lic = new LicFileLicense(this, GetKey(type));
                        }
                    }

                    if (lic != null)
                    {
                        context.SetSavedLicenseKey(type, lic.LicenseKey);
                    }
                }
            }
            return lic;
        }
示例#2
0
        /// <summary>
        ///    <para>Gets a license for the instance of the component and determines if it is valid.</para>
        /// </summary>
        public override License GetLicense(LicenseContext context, Type type, object instance, bool allowExceptions)
        {
            LicFileLicense lic = null;

            Debug.Assert(context != null, "No context provided!");
            if (context != null)
            {
                if (context.UsageMode == LicenseUsageMode.Runtime)
                {
                    string key = context.GetSavedLicenseKey(type, null);
                    if (key != null && IsKeyValid(key, type))
                    {
                        lic = new LicFileLicense(this, key);
                    }
                }

                if (lic == null)
                {
                    string modulePath = null;

                    if (context != null)
                    {
                        ITypeResolutionService resolver = (ITypeResolutionService)context.GetService(typeof(ITypeResolutionService));
                        if (resolver != null)
                        {
                            modulePath = resolver.GetPathOfAssembly(type.Assembly.GetName());
                        }
                    }

                    if (modulePath == null)
                    {
                        modulePath = type.Module.FullyQualifiedName;
                    }

                    string moduleDir   = Path.GetDirectoryName(modulePath);
                    string licenseFile = moduleDir + "\\" + type.FullName + ".lic";

                    Debug.WriteLine($"Looking for license in: {licenseFile}");
                    if (File.Exists(licenseFile))
                    {
                        Stream       licStream = new FileStream(licenseFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                        StreamReader sr        = new StreamReader(licStream);
                        string       s         = sr.ReadLine();
                        sr.Close();
                        if (IsKeyValid(s, type))
                        {
                            lic = new LicFileLicense(this, GetKey(type));
                        }
                    }

                    if (lic != null)
                    {
                        context.SetSavedLicenseKey(type, lic.LicenseKey);
                    }
                }
            }
            return(lic);
        }
        public override License GetLicense(LicenseContext context, Type type, object instance, bool allowExceptions)
        {
            LicFileLicense license = null;

            if (context != null)
            {
                if (context.UsageMode == LicenseUsageMode.Runtime)
                {
                    string savedLicenseKey = context.GetSavedLicenseKey(type, null);
                    if ((savedLicenseKey != null) && this.IsKeyValid(savedLicenseKey, type))
                    {
                        license = new LicFileLicense(this, savedLicenseKey);
                    }
                }
                if (license != null)
                {
                    return(license);
                }
                string path = null;
                if (context != null)
                {
                    ITypeResolutionService service = (ITypeResolutionService)context.GetService(typeof(ITypeResolutionService));
                    if (service != null)
                    {
                        path = service.GetPathOfAssembly(type.Assembly.GetName());
                    }
                }
                if (path == null)
                {
                    path = type.Module.FullyQualifiedName;
                }
                string str4 = Path.GetDirectoryName(path) + @"\" + type.FullName + ".lic";
                if (File.Exists(str4))
                {
                    Stream       stream = new FileStream(str4, FileMode.Open, FileAccess.Read, FileShare.Read);
                    StreamReader reader = new StreamReader(stream);
                    string       key    = reader.ReadLine();
                    reader.Close();
                    if (this.IsKeyValid(key, type))
                    {
                        license = new LicFileLicense(this, this.GetKey(type));
                    }
                }
                if (license != null)
                {
                    context.SetSavedLicenseKey(type, license.LicenseKey);
                }
            }
            return(license);
        }
	// Get the license for a type.
	public override License GetLicense
				(LicenseContext context, Type type, object instance,
				 bool allowExceptions)
			{
				String key, path;
				StreamReader reader;

				// Bail out if we don't have a license context.
				if(context == null)
				{
					return null;
				}

				// Use the saved key if we saw this type previously.
				if(context.UsageMode == LicenseUsageMode.Runtime)
				{
					key = context.GetSavedLicenseKey(type, null);
					if(key != null && IsKeyValid(key, type))
					{
						return new FileLicense(key);
					}
				}

				// Find the pathname of the assembly containing the type.
			#if CONFIG_COMPONENT_MODEL_DESIGN
				ITypeResolutionService trs;
				trs = (ITypeResolutionService)
					context.GetService(typeof(ITypeResolutionService));
				if(trs != null)
				{
					path = trs.GetPathOfAssembly(type.Assembly.GetName());
					if(path == null)
					{
						path = type.Assembly.Location;
					}
				}
				else
			#endif
				{
					path = type.Assembly.Location;
				}

				// Look for a "*.lic" file for the type.
				path = Path.Combine(Path.GetDirectoryName(path),
									type.FullName + ".lic");
				try
				{
					reader = new StreamReader(path);
				}
				catch(Exception)
				{
					// Could not open the file, so assume unlicensed.
					return null;
				}

				// Read the key from the first line of the license file.
				key = reader.ReadLine();
				reader.Close();

				// Bail out if the key is invalid.
				if(key == null || !IsKeyValid(key, type))
				{
					return null;
				}

				// Cache the key within the context.
				context.SetSavedLicenseKey(type, key);

				// Return a new file license to the caller.
				return new FileLicense(key);
			}
        /// <summary>
        /// Return the directory used to store license files
        /// </summary>
        /// <param name="context">The licence context</param>
        /// <param name="type">The type being licensed</param>
        /// <returns>The directory to look for license files</returns>
        protected virtual string GetLicenseDirectory(LicenseContext context, Type type)
        {
            string result = null;

            // try to use the type resolver service if available
            //
            if (context != null)
            {
                ITypeResolutionService resolver = (ITypeResolutionService)context.GetService(typeof(ITypeResolutionService));
                if (resolver != null)
                    result = resolver.GetPathOfAssembly(type.Assembly.GetName());
            }

            if (result == null)
            {
                // use the code base if possible - because this works properly under ASP.NET
                // where as the Module.FullyQualifiedName points to a temporary file
                //
                result = type.Assembly.CodeBase;
                if (result.StartsWith(@"file:///"))
                {
                    result = result.Replace(@"file:///", "");
                }
                else
                {
                    result = type.Module.FullyQualifiedName;
                }
            }
            return Path.GetDirectoryName(result);
        }
示例#6
0
        // Get the license for a type.
        public override License GetLicense
            (LicenseContext context, Type type, object instance,
            bool allowExceptions)
        {
            String       key, path;
            StreamReader reader;

            // Bail out if we don't have a license context.
            if (context == null)
            {
                return(null);
            }

            // Use the saved key if we saw this type previously.
            if (context.UsageMode == LicenseUsageMode.Runtime)
            {
                key = context.GetSavedLicenseKey(type, null);
                if (key != null && IsKeyValid(key, type))
                {
                    return(new FileLicense(key));
                }
            }

            // Find the pathname of the assembly containing the type.
                        #if CONFIG_COMPONENT_MODEL_DESIGN
            ITypeResolutionService trs;
            trs = (ITypeResolutionService)
                  context.GetService(typeof(ITypeResolutionService));
            if (trs != null)
            {
                path = trs.GetPathOfAssembly(type.Assembly.GetName());
                if (path == null)
                {
                    path = type.Assembly.Location;
                }
            }
            else
                        #endif
            {
                path = type.Assembly.Location;
            }

            // Look for a "*.lic" file for the type.
            path = Path.Combine(Path.GetDirectoryName(path),
                                type.FullName + ".lic");
            try
            {
                reader = new StreamReader(path);
            }
            catch (Exception)
            {
                // Could not open the file, so assume unlicensed.
                return(null);
            }

            // Read the key from the first line of the license file.
            key = reader.ReadLine();
            reader.Close();

            // Bail out if the key is invalid.
            if (key == null || !IsKeyValid(key, type))
            {
                return(null);
            }

            // Cache the key within the context.
            context.SetSavedLicenseKey(type, key);

            // Return a new file license to the caller.
            return(new FileLicense(key));
        }
        /// <include file='doc\LicFileLicenseProvider.uex' path='docs/doc[@for="LicFileLicenseProvider.GetLicense"]/*' />
        /// <devdoc>
        ///    <para>Gets a license for the instance of the component and determines if it is valid.</para>
        /// </devdoc>
        public override License GetLicense(LicenseContext context, Type type, object instance, bool allowExceptions)
        {
            ProcCountLicense lic = null;

            Debug.Assert(context != null, "No context provided!");
            //if no context is provided, do nothing
            if (context != null)
            {
                //if this control is in runtime mode
                if (context.UsageMode == LicenseUsageMode.Runtime)
                {
                    //retreive the stored license key
                    string key = context.GetSavedLicenseKey(type, null);

                    //check if the stored license key is null
                    // and call IsKeyValid to make sure its valid
                    if (key != null && IsKeyValid(key, type))
                    {
                        //if the key is valid create a new license
                        lic = new ProcCountLicense(this, key);
                    }
                }

                //if we're in design mode or
                //a suitable license key wasn't found in
                //the runtime context.
                //attempt to look for a .LIC file
                if (lic == null)
                {
                    //build up the path where the .LIC file
                    //should be
                    string modulePath = null;

                    // try and locate the file for the assembly
                    if (context != null)
                    {
                        ITypeResolutionService resolver = (ITypeResolutionService)context.GetService(typeof(ITypeResolutionService));
                        if (resolver != null)
                            modulePath = resolver.GetPathOfAssembly(type.Assembly.GetName());
                    }

                    if (modulePath == null)
                        modulePath = type.Module.FullyQualifiedName;

                    //get the path from the file location
                    string moduleDir = Path.GetDirectoryName(modulePath);

                    //build the path of the .LIC file
                    string licenseFile = moduleDir + "\\" + type.FullName + ".lic";

                    Debug.WriteLine("Path of license file: " + licenseFile);

                    //if the .LIC file exists, dig into it
                    if (File.Exists(licenseFile))
                    {
                        //crack the file and get the first line
                        Stream licStream = new FileStream(licenseFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                        StreamReader sr = new StreamReader(licStream);
                        string s = sr.ReadLine();
                        sr.Close();

                        Debug.WriteLine("Contents of license file: " + s);

                        //check if the key is valid
                        if (IsKeyValid(s, type))
                        {
                            //valid key so create a new License
                            lic = new ProcCountLicense(this, s);
                        }
                    }

                    //if we managed to create a license, stuff it into the context
                    if (lic != null)
                    {
                        context.SetSavedLicenseKey(type, lic.LicenseKey);
                    }
                }

            }
            return lic;
        }
        /// <summary>
        /// Return the directory used to store license files
        /// </summary>
        /// <param name="context">The licence context</param>
        /// <param name="type">The type being licensed</param>
        /// <returns>The directory to look for license files</returns>
        protected virtual string GetLicenseDirectory(LicenseContext context, Type type)
        {
            string result = null;

            // try to use the type resolver service if available
            //
            if (context != null && type != null)
            {
                ITypeResolutionService resolver = (ITypeResolutionService)context.GetService(typeof(ITypeResolutionService));
                if (resolver != null)
                    result = resolver.GetPathOfAssembly(type.Assembly.GetName());
            }

            if (result == null)
            {
                if (type == null)
                {
                    result = AppDomain.CurrentDomain.BaseDirectory;
                }
                else
                {
                    Assembly assembly = type.Assembly;

                    // use the code base if possible
                    //
                    result = assembly.CodeBase;
                    if (result.StartsWith(@"file:///"))
                    {
                        result = result.Replace(@"file:///", "");
                    }
                    else
                    {
                        result = type.Module.FullyQualifiedName;
                    }
                }
            }
            return Path.GetDirectoryName(result);
        }