Exemplo n.º 1
0
 /// <summary>Gets the file name of the caller assembly's embedded resource.</summary>
 /// <param name="assemblyName">The assembly name to be licensed.</param>
 /// <returns>The file name of the caller assembly's embedded resource.</returns>
 public static string GetAssemblyLicenseFileName(AssemblyName assemblyName)
 {
     return(string.Format(CultureInfo.InvariantCulture, "{0}.{1}.lic", assemblyName.Name, AssemblyLicense.GetAssemblyPublicKeyToken(assemblyName)));
 }
Exemplo n.º 2
0
        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);
        }