Exemplo n.º 1
0
            public Info Read(string assemblyFile)
            {
                Info info = new Info();

                Assembly assembly = Assembly.LoadFrom(assemblyFile);

                object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                if (attributes == null || attributes.Length != 1)
                {
                    info.Product = null;
                }
                else
                {
                    info.Product = ((AssemblyProductAttribute)attributes[0]).Product;
                }
                info.AssemblyVersion     = assembly.GetName().Version;
                info.AssemblyFileVersion = GetAssemblyFileVersion(assembly);
                info.ReleaseDate         = GetReleaseDate(info.AssemblyFileVersion);

                LicenseItemAttribute[]          licenseItemAttributes = (LicenseItemAttribute[])assembly.GetCustomAttributes(typeof(LicenseItemAttribute), false);
                KeyValuePair <string, string>[] licenseItems          = new KeyValuePair <string, string> [licenseItemAttributes.Length];
                for (int i = 0; i < licenseItemAttributes.Length; i++)
                {
                    licenseItems[i] = new KeyValuePair <string, string>(licenseItemAttributes[i].Name, licenseItemAttributes[i].GetDescription(assembly, CultureInfo.CurrentCulture));
                }
                info.LicenseItems = new ReadOnlyCollection <KeyValuePair <string, string> >(licenseItems);

                info.AssemblyData = AssemblyLicense.GetAssemblyData(assembly);
                info.PublicKeyXml = LicenseClient.PublicKeyXmlFromAssembly(assembly);

                return(info);
            }
Exemplo n.º 2
0
 public static string FormatLicenseError(LicenseError licenseError)
 {
     Debug.Assert(licenseError != null);
     return(string.Format(
                CultureInfo.CurrentUICulture,
                SR.Message_LicenseError,
                AssemblyLicense.GetAssemblyName(licenseError.Assembly),
                licenseError.Reason,
                licenseError.Message,
                licenseError.License == null ? string.Empty : licenseError.License.SignedString));
 }
Exemplo n.º 3
0
        internal static Version GetAssemblyFileVersion(Assembly assembly)
        {
            object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true);
            Version  version;

            if (attributes == null || attributes.Length == 0)
            {
                version = AssemblyLicense.GetAssemblyName(assembly).Version;
            }
            else
            {
                var attribute = (AssemblyFileVersionAttribute)attributes[0];
                version = new Version(attribute.Version);
            }
            return(version);
        }
Exemplo n.º 4
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.º 5
0
 public static string FormatNullPublicKey(Assembly assembly)
 {
     return(string.Format(CultureInfo.InvariantCulture, SR.Exception_NullPublicKey, AssemblyLicense.GetAssemblyName(assembly)));
 }
Exemplo n.º 6
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);
        }