public override string GetSavedLicenseKey(Type type, Assembly resourceAssembly) { if (_savedLicenseKeys == null || _savedLicenseKeys[type.AssemblyQualifiedName] == null) { if (_savedLicenseKeys == null) { _savedLicenseKeys = new Hashtable(); } if (resourceAssembly == null) { resourceAssembly = Assembly.GetEntryAssembly(); } if (resourceAssembly == null) { // If Assembly.EntryAssembly returns null, then we will // try everything. foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) { // Assemblies loaded in memory return empty string from Location. string location = asm.Location; if (location == string.Empty) { continue; } string fileName = new FileInfo(location).Name; Stream s = asm.GetManifestResourceStream(fileName + ".licenses"); if (s == null) { // Since the casing may be different depending on how the assembly was loaded, // we'll do a case insensitive lookup for this manifest resource stream... s = CaseInsensitiveManifestResourceStreamLookup(asm, fileName + ".licenses"); } if (s != null) { DesigntimeLicenseContextSerializer.Deserialize(s, fileName.ToUpperInvariant(), this); break; } } } else { string location = resourceAssembly.Location; if (location != string.Empty) { string fileName = Path.GetFileName(location); string licResourceName = fileName + ".licenses"; // First try the filename Stream s = resourceAssembly.GetManifestResourceStream(licResourceName); if (s == null) { string resolvedName = null; CompareInfo comparer = CultureInfo.InvariantCulture.CompareInfo; string shortAssemblyName = resourceAssembly.GetName().Name; // If the assembly has been renamed, we try our best to find a good match in the available resources // by looking at the assembly name (which doesn't change even after a file rename) + ".exe.licenses" or + ".dll.licenses" foreach (string existingName in resourceAssembly.GetManifestResourceNames()) { if (comparer.Compare(existingName, licResourceName, CompareOptions.IgnoreCase) == 0 || comparer.Compare(existingName, shortAssemblyName + ".exe.licenses", CompareOptions.IgnoreCase) == 0 || comparer.Compare(existingName, shortAssemblyName + ".dll.licenses", CompareOptions.IgnoreCase) == 0) { resolvedName = existingName; break; } } if (resolvedName != null) { s = resourceAssembly.GetManifestResourceStream(resolvedName); } } if (s != null) { DesigntimeLicenseContextSerializer.Deserialize(s, fileName.ToUpperInvariant(), this); } } } } return((string)_savedLicenseKeys[type.AssemblyQualifiedName]); }
public override string GetSavedLicenseKey(Type type, Assembly resourceAssembly) { if (savedLicenseKeys == null || savedLicenseKeys[type.AssemblyQualifiedName] == null) { Debug.WriteLineIf(s_runtimeLicenseContextSwitch.TraceVerbose, "savedLicenseKey is null or doesnt contain our type"); if (savedLicenseKeys == null) { savedLicenseKeys = new Hashtable(); } Uri licenseFile = null; // the AppDomain.CurrentDomain.SetupInformation.LicenseFile always returns null. // This means we have to find the license file using the fallback logic below. if (resourceAssembly == null) { resourceAssembly = Assembly.GetEntryAssembly(); } if (resourceAssembly == null) { Debug.WriteLineIf(s_runtimeLicenseContextSwitch.TraceVerbose, "resourceAssembly is null"); // If Assembly.EntryAssembly returns null, then we will // try everything! // foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) { // Though, I could not repro this, we seem to be hitting an AssemblyBuilder // when walking through all the assemblies in the current app domain. This throws an // exception on Assembly.CodeBase and we bail out. Catching exceptions here is not a // bad thing. if (asm.IsDynamic) { continue; } // file://fullpath/foo.exe // string fileName; // TODO NESTANDARD2.0: asm.EscapedCodeBase fileName = GetLocalPath(System.Stub.Assembly_EscapedCodeBase()); fileName = new FileInfo(fileName).Name; Stream s = asm.GetManifestResourceStream(fileName + ".licenses"); if (s == null) { //Since the casing may be different depending on how the assembly was loaded, //we'll do a case insensitive lookup for this manifest resource stream... s = CaseInsensitiveManifestResourceStreamLookup(asm, fileName + ".licenses"); } if (s != null) { DesigntimeLicenseContextSerializer.Deserialize(s, fileName.ToUpper(CultureInfo.InvariantCulture), this); break; } } } else if (!resourceAssembly.IsDynamic) { // EscapedCodeBase won't be supported by emitted assemblies anyway Debug.WriteLineIf(s_runtimeLicenseContextSwitch.TraceVerbose, "resourceAssembly is not null"); string fileName; //TODO NETSTANDARD2.0: resourceAssembly.EscapedCodeBase fileName = GetLocalPath(System.Stub.Assembly_EscapedCodeBase()); fileName = Path.GetFileName(fileName); // we don't want to use FileInfo here... it requests FileIOPermission that we // might now have... see VSWhidbey 527758 string licResourceName = fileName + ".licenses"; // first try the filename Stream s = resourceAssembly.GetManifestResourceStream(licResourceName); if (s == null) { string resolvedName = null; CompareInfo comparer = CultureInfo.InvariantCulture.CompareInfo; string shortAssemblyName = resourceAssembly.GetName().Name; // if the assembly has been renamed, we try our best to find a good match in the available resources // by looking at the assembly name (which doesn't change even after a file rename) + ".exe.licenses" or + ".dll.licenses" foreach (String existingName in resourceAssembly.GetManifestResourceNames()) { if (comparer.Compare(existingName, licResourceName, CompareOptions.IgnoreCase) == 0 || comparer.Compare(existingName, shortAssemblyName + ".exe.licenses", CompareOptions.IgnoreCase) == 0 || comparer.Compare(existingName, shortAssemblyName + ".dll.licenses", CompareOptions.IgnoreCase) == 0) { resolvedName = existingName; break; } } if (resolvedName != null) { s = resourceAssembly.GetManifestResourceStream(resolvedName); } } if (s != null) { DesigntimeLicenseContextSerializer.Deserialize(s, fileName.ToUpper(CultureInfo.InvariantCulture), this); } } if (licenseFile != null) { Debug.WriteLineIf(s_runtimeLicenseContextSwitch.TraceVerbose, "licenseFile: " + licenseFile.ToString()); Debug.WriteLineIf(s_runtimeLicenseContextSwitch.TraceVerbose, "opening licenses file over URI " + licenseFile.ToString()); Stream s = OpenRead(licenseFile); if (s != null) { string[] segments = licenseFile.Segments; string licFileName = segments[segments.Length - 1]; string key = licFileName.Substring(0, licFileName.LastIndexOf(".")); DesigntimeLicenseContextSerializer.Deserialize(s, key.ToUpper(CultureInfo.InvariantCulture), this); } } } Debug.WriteLineIf(s_runtimeLicenseContextSwitch.TraceVerbose, "returning : " + (string)savedLicenseKeys[type.AssemblyQualifiedName]); return((string)savedLicenseKeys[type.AssemblyQualifiedName]); }
public override string GetSavedLicenseKey(Type type, Assembly resourceAssembly) { if (savedLicenseKeys == null) { Uri licenseFile = null; if (resourceAssembly == null) { string rawFile = (string)AppDomain.CurrentDomain.SetupInformation.LicenseFile; string codeBase; // FileIOPermission is required for ApplicationBase in URL-hosted domains // see ASURT 101244 FileIOPermission perm = new FileIOPermission(PermissionState.Unrestricted); perm.Assert(); try { codeBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; } finally { CodeAccessPermission.RevertAssert(); } if (rawFile != null && codeBase != null) { licenseFile = new Uri(new Uri(codeBase), rawFile); } } if (licenseFile == null && resourceAssembly == null) { resourceAssembly = Assembly.GetEntryAssembly(); if (resourceAssembly == null) { // If Assembly.EntryAssembly returns null, then we will // try everything! // foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) { // Bug# 78962: Though, I could not repro this, we seem to be hitting an AssemblyBuilder // when walking through all the assemblies in the current app domain. This throws an // exception on Assembly.CodeBase and we bail out. Catching exceptions here is not a // bad thing. if (asm is AssemblyBuilder) { continue; } // file://fullpath/foo.exe // string fileName = GetLocalPath(asm.EscapedCodeBase); fileName = new FileInfo(fileName).Name; Stream s = asm.GetManifestResourceStream(fileName + ".licenses"); if (s == null) { //Since the casing may be different depending on how the assembly was loaded, //we'll do a case insensitive lookup for this manifest resource stream... s = CaseInsensitiveManifestResourceStreamLookup(asm, fileName + ".licenses"); } if (s != null) { DesigntimeLicenseContextSerializer.Deserialize(s, fileName.ToUpper(CultureInfo.InvariantCulture), this); break; } } } else { string fileName = GetLocalPath(resourceAssembly.EscapedCodeBase); fileName = new FileInfo(fileName).Name; string licResourceName = fileName + ".licenses"; Stream s = resourceAssembly.GetManifestResourceStream(licResourceName); if (s == null) { string resolvedName = null; CompareInfo comparer = CultureInfo.InvariantCulture.CompareInfo; foreach (String existingName in resourceAssembly.GetManifestResourceNames()) { if (comparer.Compare(existingName, licResourceName, CompareOptions.IgnoreCase) == 0) { resolvedName = existingName; break; } } if (resolvedName != null) { s = resourceAssembly.GetManifestResourceStream(resolvedName); } } if (s != null) { DesigntimeLicenseContextSerializer.Deserialize(s, fileName.ToUpper(CultureInfo.InvariantCulture), this); } } } if (licenseFile != null && savedLicenseKeys == null) { Stream s = OpenRead(licenseFile); if (s != null) { string[] segments = licenseFile.Segments; string licFileName = segments[segments.Length - 1]; string key = licFileName.Substring(0, licFileName.LastIndexOf(".")); DesigntimeLicenseContextSerializer.Deserialize(s, key.ToUpper(CultureInfo.InvariantCulture), this); } } if (savedLicenseKeys == null) { savedLicenseKeys = new Hashtable(); } } return((string)savedLicenseKeys[type.AssemblyQualifiedName]); }
public override string GetSavedLicenseKey(Type type, Assembly resourceAssembly) { if (_savedLicenseKeys == null || _savedLicenseKeys[type.AssemblyQualifiedName] == null) { Debug.WriteLineIf(s_runtimeLicenseContextSwitch.TraceVerbose, "savedLicenseKey is null or doesn't contain our type"); if (_savedLicenseKeys == null) { _savedLicenseKeys = new Hashtable(); } if (resourceAssembly == null) { resourceAssembly = Assembly.GetEntryAssembly(); } if (resourceAssembly == null) { Debug.WriteLineIf(s_runtimeLicenseContextSwitch.TraceVerbose, "resourceAssembly is null"); // If Assembly.EntryAssembly returns null, then we will // try everything. foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) { // Though, I could not repro this, we seem to be hitting an AssemblyBuilder // when walking through all the assemblies in the current app domain. This throws an // exception on Assembly.CodeBase and we bail out. Catching exceptions here is not a // bad thing. if (asm.IsDynamic) { continue; } // file://fullpath/foo.exe string fileName = GetLocalPath(asm.EscapedCodeBase); fileName = new FileInfo(fileName).Name; Stream s = asm.GetManifestResourceStream(fileName + ".licenses"); if (s == null) { // Since the casing may be different depending on how the assembly was loaded, // we'll do a case insensitive lookup for this manifest resource stream... s = CaseInsensitiveManifestResourceStreamLookup(asm, fileName + ".licenses"); } if (s != null) { DesigntimeLicenseContextSerializer.Deserialize(s, fileName.ToUpper(CultureInfo.InvariantCulture), this); break; } } } else if (!resourceAssembly.IsDynamic) { // EscapedCodeBase won't be supported by emitted assemblies anyway Debug.WriteLineIf(s_runtimeLicenseContextSwitch.TraceVerbose, "resourceAssembly is not null"); string fileName; fileName = GetLocalPath(resourceAssembly.EscapedCodeBase); fileName = Path.GetFileName(fileName); string licResourceName = fileName + ".licenses"; // First try the filename Stream s = resourceAssembly.GetManifestResourceStream(licResourceName); if (s == null) { string resolvedName = null; CompareInfo comparer = CultureInfo.InvariantCulture.CompareInfo; string shortAssemblyName = resourceAssembly.GetName().Name; // If the assembly has been renamed, we try our best to find a good match in the available resources // by looking at the assembly name (which doesn't change even after a file rename) + ".exe.licenses" or + ".dll.licenses" foreach (string existingName in resourceAssembly.GetManifestResourceNames()) { if (comparer.Compare(existingName, licResourceName, CompareOptions.IgnoreCase) == 0 || comparer.Compare(existingName, shortAssemblyName + ".exe.licenses", CompareOptions.IgnoreCase) == 0 || comparer.Compare(existingName, shortAssemblyName + ".dll.licenses", CompareOptions.IgnoreCase) == 0) { resolvedName = existingName; break; } } if (resolvedName != null) { s = resourceAssembly.GetManifestResourceStream(resolvedName); } } if (s != null) { DesigntimeLicenseContextSerializer.Deserialize(s, fileName.ToUpper(CultureInfo.InvariantCulture), this); } } } Debug.WriteLineIf(s_runtimeLicenseContextSwitch.TraceVerbose, $"returning : {(string)_savedLicenseKeys[type.AssemblyQualifiedName]}"); return((string)_savedLicenseKeys[type.AssemblyQualifiedName]); }
public override string GetSavedLicenseKey(Type type, Assembly resourceAssembly) { if ((this.savedLicenseKeys == null) || (this.savedLicenseKeys[type.AssemblyQualifiedName] == null)) { if (this.savedLicenseKeys == null) { this.savedLicenseKeys = new Hashtable(); } Uri resourceUri = null; if (resourceAssembly == null) { string applicationBase; string licenseFile = AppDomain.CurrentDomain.SetupInformation.LicenseFile; new FileIOPermission(PermissionState.Unrestricted).Assert(); try { applicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; } finally { CodeAccessPermission.RevertAssert(); } if ((licenseFile != null) && (applicationBase != null)) { resourceUri = new Uri(new Uri(applicationBase), licenseFile); } } if (resourceUri == null) { if (resourceAssembly == null) { resourceAssembly = Assembly.GetEntryAssembly(); } if (resourceAssembly == null) { foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { if (!assembly.IsDynamic) { string localPath; new FileIOPermission(PermissionState.Unrestricted).Assert(); try { localPath = this.GetLocalPath(assembly.EscapedCodeBase); localPath = new FileInfo(localPath).Name; } finally { CodeAccessPermission.RevertAssert(); } Stream manifestResourceStream = assembly.GetManifestResourceStream(localPath + ".licenses"); if (manifestResourceStream == null) { manifestResourceStream = this.CaseInsensitiveManifestResourceStreamLookup(assembly, localPath + ".licenses"); } if (manifestResourceStream != null) { DesigntimeLicenseContextSerializer.Deserialize(manifestResourceStream, localPath.ToUpper(CultureInfo.InvariantCulture), this); break; } } } } else if (!resourceAssembly.IsDynamic) { string fileName; new FileIOPermission(PermissionState.Unrestricted).Assert(); try { fileName = this.GetLocalPath(resourceAssembly.EscapedCodeBase); } finally { CodeAccessPermission.RevertAssert(); } fileName = Path.GetFileName(fileName); string name = fileName + ".licenses"; Stream o = resourceAssembly.GetManifestResourceStream(name); if (o == null) { string str6 = null; CompareInfo compareInfo = CultureInfo.InvariantCulture.CompareInfo; string str7 = resourceAssembly.GetName().Name; foreach (string str8 in resourceAssembly.GetManifestResourceNames()) { if (((compareInfo.Compare(str8, name, CompareOptions.IgnoreCase) == 0) || (compareInfo.Compare(str8, str7 + ".exe.licenses", CompareOptions.IgnoreCase) == 0)) || (compareInfo.Compare(str8, str7 + ".dll.licenses", CompareOptions.IgnoreCase) == 0)) { str6 = str8; break; } } if (str6 != null) { o = resourceAssembly.GetManifestResourceStream(str6); } } if (o != null) { DesigntimeLicenseContextSerializer.Deserialize(o, fileName.ToUpper(CultureInfo.InvariantCulture), this); } } } if (resourceUri != null) { Stream stream3 = OpenRead(resourceUri); if (stream3 != null) { string[] segments = resourceUri.Segments; string str9 = segments[segments.Length - 1]; string str10 = str9.Substring(0, str9.LastIndexOf(".")); DesigntimeLicenseContextSerializer.Deserialize(stream3, str10.ToUpper(CultureInfo.InvariantCulture), this); } } } return((string)this.savedLicenseKeys[type.AssemblyQualifiedName]); }