/// <exclude />
        protected override LicenseProviderResult Load()
        {
            try
            {
                if (!File.Exists(FullPath))
                {
                    return(LicenseProviderResult.FromErrorMessage(Messages.FileNotFound));
                }

                string license = File.ReadAllText(FullPath);
                if (string.IsNullOrEmpty(license))
                {
                    return(LicenseProviderResult.FromErrorMessage(Messages.EmptyFile));
                }
                else
                {
                    return(LicenseProviderResult.FromLicense(license));
                }
            }
            catch (IOException exception)
            {
                return(LicenseProviderResult.FromErrorMessage(Messages.FormatReadFileFailed(exception.Message)));
            }
            catch (UnauthorizedAccessException exception)
            {
                return(LicenseProviderResult.FromErrorMessage(Messages.FormatReadFileFailed(exception.Message)));
            }
            catch (SecurityException exception)
            {
                return(LicenseProviderResult.FromErrorMessage(Messages.FormatReadFileFailed(exception.Message)));
            }
        }
Esempio n. 2
0
        private LicenseProviderResult GetLicenseFromEntryAssembly()
        {
            if (_cachedResult.IsEmpty)
            {
                _cachedResult = LoadFromEntryAssembly();
            }

            return(_cachedResult);
        }
 /// <exclude />
 protected internal override string GetTraceMessage(LicenseProviderResult result)
 {
     if (result.License == null)
     {
         return(string.Format(CultureInfo.InvariantCulture, "{0} - {1}", TraceString, result.ErrorMessage));
     }
     else
     {
         return(string.Format(CultureInfo.InvariantCulture, "{0}\n{1}", TraceString, result.License));
     }
 }
        protected internal sealed override LicenseProviderResult ProvideLicense()
        {
            if (_result.IsEmpty)
            {
                _result = Load();
                if (_result.IsEmpty)
                {
                    throw new InvalidOperationException(ExceptionMessages.EmptyLicenseProviderResult);
                }
            }

            return(_result);
        }
Esempio n. 5
0
        /// <exclude />
        protected internal override string GetTraceMessage(LicenseProviderResult result)
        {
            StringBuilder stringBuilder = new StringBuilder();

            if (result.License == null)
            {
                stringBuilder.Append(string.Format(CultureInfo.InvariantCulture, "{0} - {1}", TraceString, result.ErrorMessage));
            }
            else
            {
                stringBuilder.Append(TraceString);
            }

            if (result.Data != null)
            {
                if (result.Data is Assembly)
                {
                    Assembly assembly = result.Data as Assembly;
                    stringBuilder.AppendLine();
                    if (EntryAssemblyOnly)
                    {
                        stringBuilder.Append(string.Format(CultureInfo.InvariantCulture, @"Assembly=""{0}"", EntryAssembly=""{1}""", Assembly, assembly));
                    }
                    else
                    {
                        stringBuilder.Append(string.Format(CultureInfo.InvariantCulture, @"Assembly=""{0}"", CallingAssembly=""{1}""", Assembly, assembly));
                    }
                }
                else
                {
                    List <StackFrame> stackFrames = (List <StackFrame>)result.Data;
                    foreach (StackFrame stackFrame in stackFrames)
                    {
                        stringBuilder.AppendLine();
                        MethodBase method   = stackFrame.GetMethod();
                        Assembly   assembly = stackFrame.GetMethod().ReflectedType.Assembly;
                        stringBuilder.Append(string.Format(CultureInfo.InvariantCulture, @"Method=""{0}.{1}"", Assembly=""{2}""", method.ReflectedType, method.Name, assembly));
                    }
                }
            }

            if (result.License != null)
            {
                stringBuilder.AppendLine();
                stringBuilder.Append(result.License);
            }
            return(stringBuilder.ToString());
        }
        /// <exclude />
        protected override LicenseProviderResult Load()
        {
            bool isGranted = false;

            try
            {
                string             subKey     = SubkeyNameToOpen;
                RegistryPermission permission = new RegistryPermission(PermissionState.Unrestricted);
                isGranted = SecurityManager.IsGranted(permission);
                if (isGranted)
                {
                    permission.Assert();
                }
                using (RegistryKey registryKey = RegistryKey.OpenSubKey(subKey))
                {
                    if (registryKey == null)
                    {
                        return(LicenseProviderResult.FromErrorMessage(Messages.OpenRegistryKeyFailed));
                    }

                    string license = (string)registryKey.GetValue(ValueName, null);
                    if (string.IsNullOrEmpty(license))
                    {
                        return(LicenseProviderResult.FromErrorMessage(Messages.EmptyRegistryValue));
                    }
                    else
                    {
                        return(LicenseProviderResult.FromLicense(license));
                    }
                }
            }
            catch (SecurityException exception)
            {
                return(LicenseProviderResult.FromErrorMessage(Messages.FormatReadRegistryFailed(exception.Message)));
            }
            finally
            {
                if (isGranted)
                {
                    CodeAccessPermission.RevertAssert();
                }
            }
        }
Esempio n. 7
0
        private LicenseProviderResult GetLicenseFromCallingAssembly()
        {
            List <StackFrame> stackFrames = IsTraceEnabled ? new List <StackFrame>() : null;
            StackTrace        stackTrace  = new StackTrace();

            for (int i = 0; i < stackTrace.FrameCount; i++)
            {
                StackFrame stackFrame    = stackTrace.GetFrame(i);
                Type       reflectedType = stackFrame.GetMethod().ReflectedType;
                if (reflectedType == null)
                {
                    continue;
                }
                Assembly callingAssembly = reflectedType.Assembly;
                if (callingAssembly == Assembly)
                {
                    continue;
                }

                if (IsTraceEnabled)
                {
                    stackFrames.Add(stackFrame);
                }
                string license;
                if (CachedLicenses.ContainsKey(callingAssembly))
                {
                    license = CachedLicenses[callingAssembly];
                }
                else
                {
                    license = GetLicenseFromAssembly(callingAssembly);
                    CachedLicenses.Add(callingAssembly, license);
                }

                if (license != null)
                {
                    return(LicenseProviderResult.FromLicense(license, callingAssembly));
                }
            }

            return(LicenseProviderResult.FromErrorMessage(Messages.FormatEmbeddedResourceNotFound(AssemblyLicenseFileName), stackFrames));
        }
Esempio n. 8
0
        private LicenseProviderResult LoadFromEntryAssembly()
        {
            Assembly entryAssembly = Assembly.GetEntryAssembly();

            if (entryAssembly == null)
            {
                return(LicenseProviderResult.FromErrorMessage(Messages.FormatEmbeddedResourceNotFound(AssemblyLicenseFileName)));
            }

            string license = GetLicenseFromAssembly(entryAssembly);

            if (string.IsNullOrEmpty(license))
            {
                return(LicenseProviderResult.FromErrorMessage(Messages.FormatEmbeddedResourceNotFound(AssemblyLicenseFileName)));
            }
            else
            {
                return(LicenseProviderResult.FromLicense(license, entryAssembly));
            }
        }
        protected override LicenseProviderResult Load()
        {
            try
            {
                using (IsolatedStorageFile store = IsolatedStorageFile.GetStore(Scope, ApplicationEvidenceType))
                {
                    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(Path, FileMode.Open, FileAccess.Read, store))
                    {
                        if (stream == null)
                        {
                            return(LicenseProviderResult.FromErrorMessage(Messages.FileNotFound));
                        }

                        using (StreamReader reader = new StreamReader(stream))
                        {
                            string xaml = reader.ReadToEnd();
                            if (string.IsNullOrEmpty(xaml))
                            {
                                return(LicenseProviderResult.FromErrorMessage(Messages.EmptyFile));
                            }
                            else
                            {
                                return(LicenseProviderResult.FromLicense(reader.ReadToEnd()));
                            }
                        }
                    }
                }
            }
            catch (FileNotFoundException)
            {
                return(LicenseProviderResult.FromErrorMessage(Messages.FileNotFound));
            }
            catch (SecurityException exception)
            {
                return(LicenseProviderResult.FromErrorMessage(Messages.FormatReadFileFailed(exception.Message)));
            }
            catch (IsolatedStorageException exception)
            {
                return(LicenseProviderResult.FromErrorMessage(Messages.FormatReadFileFailed(exception.Message)));
            }
        }
Esempio n. 10
0
        private License ProvideLicense(bool designMode)
        {
            List <LicenseProviderAttribute> providers = designMode ? _designTimeProviders : _runtimeProviders;

            foreach (LicenseProviderAttribute provider in providers)
            {
                // Check provider.Assembly to prevent it from being changed by reflection
                if (provider.Assembly != Assembly)
                {
                    throw new InvalidOperationException();
                }

                LicenseProviderResult result = provider.ProvideLicense();
                if (result.IsEmpty)
                {
                    throw new InvalidOperationException(ExceptionMessages.EmptyLicenseProviderResult);
                }

                if (provider.IsTraceEnabled)
                {
                    WriteTrace(provider, result);
                }

                License license = LoadLicense(result.License, result.Data);
                if (license == null)
                {
                    continue;
                }

                license.Freeze(provider);

                return(license);
            }

            return(null);
        }
Esempio n. 11
0
 private static void WriteTrace(LicenseProviderAttribute licenseProvider, LicenseProviderResult result)
 {
     Debug.Assert(licenseProvider.IsTraceEnabled);
     Trace.WriteLine(licenseProvider.GetTraceMessage(result), GetTraceCategory(licenseProvider.Assembly));
 }
Esempio n. 12
0
 /// <summary>Gets the diagnostics trace message.</summary>
 /// <param name="result">The result of this license provider.</param>
 /// <returns>The diagnostics trace message</returns>
 protected internal abstract string GetTraceMessage(LicenseProviderResult result);
 protected internal sealed override void Reset()
 {
     _result = new LicenseProviderResult();
 }