コード例 #1
0
ファイル: LicenseManager.cs プロジェクト: DevZest/Licensing
        private static LicenseError Validate(string licenseItemName, Assembly assembly, bool designMode, bool throwsException)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }

            if (string.IsNullOrEmpty(licenseItemName))
            {
                throw new ArgumentNullException("licenseItemName");
            }

            LicenseError error   = null;
            License      license = GetLicense(assembly, designMode);

            if (license != null)
            {
                error = license.Validate(licenseItemName);
            }

            if (license == null || error != null)
            {
                string publicKeyToken = AssemblyLicense.GetAssemblyPublicKeyToken(assembly);

                // check the calling assembly's public key token
                Assembly entryAssembly = Assembly.GetEntryAssembly();
                if (entryAssembly != null && entryAssembly != assembly)
                {
                    if (AssemblyLicense.GetAssemblyPublicKeyToken(entryAssembly) == publicKeyToken) // Entry assembly signed by the same key
                    {
                        if (IsTraceEnabled)
                        {
                            WriteTrace(assembly, Messages.FormatAssemblySignedWithSameKey(entryAssembly));
                        }
                        return(null);
                    }
                }

                // check the calling assembly's public key token
                Assembly currentAssembly = Assembly.GetExecutingAssembly();

                StackTrace stackTrace = new StackTrace();
                for (int i = 1; i < stackTrace.FrameCount; i++)
                {
                    StackFrame stackFrame    = stackTrace.GetFrame(i);
                    Type       reflectedType = stackFrame.GetMethod().ReflectedType;
                    if (reflectedType == null)
                    {
                        continue;
                    }
                    Assembly reflectedAssembly = reflectedType.Assembly;
                    if (reflectedAssembly == assembly || reflectedAssembly == currentAssembly)
                    {
                        continue;
                    }
                    if (AssemblyLicense.GetAssemblyPublicKeyToken(reflectedAssembly) == publicKeyToken) // Calling assembly signed by the same key
                    {
                        if (IsTraceEnabled)
                        {
                            WriteTrace(assembly, Messages.FormatAssemblySignedWithSameKey(reflectedAssembly));
                        }
                        return(null);
                    }
                }
            }

            if (license == null)
            {
                error = new LicenseError(assembly, LicenseErrorReason.NullLicense, Messages.NullLicense, null);
            }

            if (error != null && throwsException)
            {
                throw new LicenseException(error);
            }

            return(error);
        }
コード例 #2
0
        /// <summary>Initializes a new instance of the <see cref="LicenseError" /> class</summary>
        /// <param name="assembly">The assembly for the license validation.</param>
        /// <param name="reason">The reason for the license validation error.</param>
        /// <param name="message">The message for the license validation error.</param>
        /// <param name="license">The <see cref="License" /> that fails the validation.</param>
        public LicenseError(Assembly assembly, LicenseErrorReason reason, string message, License license)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }

            if (string.IsNullOrEmpty(message))
            {
                throw new ArgumentNullException("message");
            }

            _assembly = assembly;
            _reason   = reason;
            _message  = message;
            _license  = license;
        }
コード例 #3
0
 internal LicenseItemCollection(License license)
 {
     Debug.Assert(license != null);
     _licence = license;
 }