Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / CompMod / System / ComponentModel / Design / DesigntimeLicenseContext.cs / 1305376 / DesigntimeLicenseContext.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.ComponentModel.Design {
using System.Runtime.Remoting;
using System.Diagnostics;
using System;
using Microsoft.Win32;
using System.Net;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Security;
using System.Security.Permissions;
///
///
/// Provides design-time support for licensing.
///
///
[HostProtection(SharedState = true)]
[System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.InheritanceDemand, Name = "FullTrust")]
[System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.LinkDemand, Name="FullTrust")]
public class DesigntimeLicenseContext : LicenseContext {
internal Hashtable savedLicenseKeys = new Hashtable();
///
///
/// Gets or sets the license usage mode.
///
///
public override LicenseUsageMode UsageMode {
get {
return LicenseUsageMode.Designtime;
}
}
///
///
/// Gets a saved license key.
///
///
public override string GetSavedLicenseKey(Type type, Assembly resourceAssembly) {
return null;
}
///
///
/// Sets a saved license key.
///
///
public override void SetSavedLicenseKey(Type type, string key) {
savedLicenseKeys[type.AssemblyQualifiedName] = key;
}
}
[HostProtection(SharedState = true)]
internal class RuntimeLicenseContext : LicenseContext
{
private static TraceSwitch RuntimeLicenseContextSwitch = new TraceSwitch("RuntimeLicenseContextTrace", "RuntimeLicenseContext tracing");
const int ReadBlock = 400;
internal Hashtable savedLicenseKeys;
///
/// This method takes a file URL and converts it to a local path. The trick here is that
/// if there is a '#' in the path, everything after this is treated as a fragment. So
/// we need to append the fragment to the end of the path.
///
private string GetLocalPath(string fileName) {
System.Diagnostics.Debug.Assert(fileName != null && fileName.Length > 0, "Cannot get local path, fileName is not valid");
Uri uri = new Uri(fileName);
return uri.LocalPath + uri.Fragment;
}
public override string GetSavedLicenseKey(Type type, Assembly resourceAssembly) {
if (savedLicenseKeys == null || savedLicenseKeys[type.AssemblyQualifiedName]==null) {
Debug.WriteLineIf(RuntimeLicenseContextSwitch.TraceVerbose, "savedLicenseKey is null or doesnt contain our type");
if (savedLicenseKeys == null) {
savedLicenseKeys = new Hashtable();
}
Uri licenseFile = null;
if (resourceAssembly == null) {
Debug.WriteLineIf(RuntimeLicenseContextSwitch.TraceVerbose,"resourceAssembly is null");
string rawFile = (string)AppDomain.CurrentDomain.SetupInformation.LicenseFile;
Debug.WriteLineIf(RuntimeLicenseContextSwitch.TraceVerbose,"rawfile: " + rawFile);
string codeBase;
// FileIOPermission is required for ApplicationBase in URL-hosted domains
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) {
if(resourceAssembly == null) {
resourceAssembly = Assembly.GetEntryAssembly();
}
if (resourceAssembly == null) {
Debug.WriteLineIf(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;
FileIOPermission perm = new FileIOPermission(PermissionState.Unrestricted);
perm.Assert();
try
{
fileName = GetLocalPath(asm.EscapedCodeBase);
fileName = new FileInfo(fileName).Name;
}
finally
{
CodeAccessPermission.RevertAssert();
}
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(RuntimeLicenseContextSwitch.TraceVerbose,"resourceAssembly is not null");
string fileName;
FileIOPermission perm = new FileIOPermission(PermissionState.Unrestricted);
perm.Assert();
try
{
fileName = GetLocalPath(resourceAssembly.EscapedCodeBase);
}
finally
{
CodeAccessPermission.RevertAssert();
}
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(RuntimeLicenseContextSwitch.TraceVerbose,"licenseFile: " + licenseFile.ToString());
Debug.WriteLineIf(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(RuntimeLicenseContextSwitch.TraceVerbose,"returning : " + (string)savedLicenseKeys[type.AssemblyQualifiedName]);
return(string)savedLicenseKeys[type.AssemblyQualifiedName];
}
/**
* Looks up a .licenses file in the assembly manifest using
* case-insensitive lookup rules. We do this because the name
* we are attempting to locate could have different casing
* depending on how the assembly was loaded.
**/
private Stream CaseInsensitiveManifestResourceStreamLookup(Assembly satellite, string name)
{
CompareInfo comparer = CultureInfo.InvariantCulture.CompareInfo;
//loop through the resource names in the assembly
// we try to handle the case where the assembly file name has been renamed
// by trying to guess the original file name based on the assembly name...
string assemblyShortName = satellite.GetName().Name;
foreach(string existingName in satellite.GetManifestResourceNames()) {
if (comparer.Compare(existingName, name, CompareOptions.IgnoreCase) == 0 ||
comparer.Compare(existingName, assemblyShortName + ".exe.licenses") == 0 ||
comparer.Compare(existingName, assemblyShortName + ".dll.licenses") == 0) {
name = existingName;
break;
}
}
//finally, attempt to return our stream based on the
//case insensitive match we found
//
return satellite.GetManifestResourceStream(name);
}
static Stream OpenRead(Uri resourceUri) {
Stream result = null;
PermissionSet perms = new PermissionSet(PermissionState.Unrestricted);
perms.Assert();
try {
WebClient webClient = new WebClient();
webClient.Credentials = CredentialCache.DefaultCredentials;
result = webClient.OpenRead(resourceUri.ToString());
}
catch (Exception e) {
Debug.Fail(e.ToString());
}
finally {
CodeAccessPermission.RevertAssert();
}
return result;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.ComponentModel.Design {
using System.Runtime.Remoting;
using System.Diagnostics;
using System;
using Microsoft.Win32;
using System.Net;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Security;
using System.Security.Permissions;
///
///
/// Provides design-time support for licensing.
///
///
[HostProtection(SharedState = true)]
[System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.InheritanceDemand, Name = "FullTrust")]
[System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.LinkDemand, Name="FullTrust")]
public class DesigntimeLicenseContext : LicenseContext {
internal Hashtable savedLicenseKeys = new Hashtable();
///
///
/// Gets or sets the license usage mode.
///
///
public override LicenseUsageMode UsageMode {
get {
return LicenseUsageMode.Designtime;
}
}
///
///
/// Gets a saved license key.
///
///
public override string GetSavedLicenseKey(Type type, Assembly resourceAssembly) {
return null;
}
///
///
/// Sets a saved license key.
///
///
public override void SetSavedLicenseKey(Type type, string key) {
savedLicenseKeys[type.AssemblyQualifiedName] = key;
}
}
[HostProtection(SharedState = true)]
internal class RuntimeLicenseContext : LicenseContext
{
private static TraceSwitch RuntimeLicenseContextSwitch = new TraceSwitch("RuntimeLicenseContextTrace", "RuntimeLicenseContext tracing");
const int ReadBlock = 400;
internal Hashtable savedLicenseKeys;
///
/// This method takes a file URL and converts it to a local path. The trick here is that
/// if there is a '#' in the path, everything after this is treated as a fragment. So
/// we need to append the fragment to the end of the path.
///
private string GetLocalPath(string fileName) {
System.Diagnostics.Debug.Assert(fileName != null && fileName.Length > 0, "Cannot get local path, fileName is not valid");
Uri uri = new Uri(fileName);
return uri.LocalPath + uri.Fragment;
}
public override string GetSavedLicenseKey(Type type, Assembly resourceAssembly) {
if (savedLicenseKeys == null || savedLicenseKeys[type.AssemblyQualifiedName]==null) {
Debug.WriteLineIf(RuntimeLicenseContextSwitch.TraceVerbose, "savedLicenseKey is null or doesnt contain our type");
if (savedLicenseKeys == null) {
savedLicenseKeys = new Hashtable();
}
Uri licenseFile = null;
if (resourceAssembly == null) {
Debug.WriteLineIf(RuntimeLicenseContextSwitch.TraceVerbose,"resourceAssembly is null");
string rawFile = (string)AppDomain.CurrentDomain.SetupInformation.LicenseFile;
Debug.WriteLineIf(RuntimeLicenseContextSwitch.TraceVerbose,"rawfile: " + rawFile);
string codeBase;
// FileIOPermission is required for ApplicationBase in URL-hosted domains
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) {
if(resourceAssembly == null) {
resourceAssembly = Assembly.GetEntryAssembly();
}
if (resourceAssembly == null) {
Debug.WriteLineIf(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;
FileIOPermission perm = new FileIOPermission(PermissionState.Unrestricted);
perm.Assert();
try
{
fileName = GetLocalPath(asm.EscapedCodeBase);
fileName = new FileInfo(fileName).Name;
}
finally
{
CodeAccessPermission.RevertAssert();
}
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(RuntimeLicenseContextSwitch.TraceVerbose,"resourceAssembly is not null");
string fileName;
FileIOPermission perm = new FileIOPermission(PermissionState.Unrestricted);
perm.Assert();
try
{
fileName = GetLocalPath(resourceAssembly.EscapedCodeBase);
}
finally
{
CodeAccessPermission.RevertAssert();
}
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(RuntimeLicenseContextSwitch.TraceVerbose,"licenseFile: " + licenseFile.ToString());
Debug.WriteLineIf(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(RuntimeLicenseContextSwitch.TraceVerbose,"returning : " + (string)savedLicenseKeys[type.AssemblyQualifiedName]);
return(string)savedLicenseKeys[type.AssemblyQualifiedName];
}
/**
* Looks up a .licenses file in the assembly manifest using
* case-insensitive lookup rules. We do this because the name
* we are attempting to locate could have different casing
* depending on how the assembly was loaded.
**/
private Stream CaseInsensitiveManifestResourceStreamLookup(Assembly satellite, string name)
{
CompareInfo comparer = CultureInfo.InvariantCulture.CompareInfo;
//loop through the resource names in the assembly
// we try to handle the case where the assembly file name has been renamed
// by trying to guess the original file name based on the assembly name...
string assemblyShortName = satellite.GetName().Name;
foreach(string existingName in satellite.GetManifestResourceNames()) {
if (comparer.Compare(existingName, name, CompareOptions.IgnoreCase) == 0 ||
comparer.Compare(existingName, assemblyShortName + ".exe.licenses") == 0 ||
comparer.Compare(existingName, assemblyShortName + ".dll.licenses") == 0) {
name = existingName;
break;
}
}
//finally, attempt to return our stream based on the
//case insensitive match we found
//
return satellite.GetManifestResourceStream(name);
}
static Stream OpenRead(Uri resourceUri) {
Stream result = null;
PermissionSet perms = new PermissionSet(PermissionState.Unrestricted);
perms.Assert();
try {
WebClient webClient = new WebClient();
webClient.Credentials = CredentialCache.DefaultCredentials;
result = webClient.OpenRead(resourceUri.ToString());
}
catch (Exception e) {
Debug.Fail(e.ToString());
}
finally {
CodeAccessPermission.RevertAssert();
}
return result;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- TextServicesContext.cs
- BindMarkupExtensionSerializer.cs
- MenuScrollingVisibilityConverter.cs
- TreeViewImageIndexConverter.cs
- StatusBarPanelClickEvent.cs
- QilInvokeLateBound.cs
- FrameworkContextData.cs
- ViewManager.cs
- ForceCopyBuildProvider.cs
- COM2ComponentEditor.cs
- LocalizationParserHooks.cs
- ErrorWrapper.cs
- GenericXmlSecurityTokenAuthenticator.cs
- DataSourceHelper.cs
- Switch.cs
- DataGridViewAccessibleObject.cs
- VisualBasicExpressionConverter.cs
- CodeSubDirectoriesCollection.cs
- CharAnimationUsingKeyFrames.cs
- SharedUtils.cs
- MimeMultiPart.cs
- EditorAttributeInfo.cs
- DetailsViewUpdateEventArgs.cs
- BitmapCache.cs
- ConfigurationLocationCollection.cs
- Decoder.cs
- Hex.cs
- PanelStyle.cs
- XslTransform.cs
- UnsafeNativeMethods.cs
- CFStream.cs
- TypeListConverter.cs
- EntityDataSourceMemberPath.cs
- ArrayElementGridEntry.cs
- RestrictedTransactionalPackage.cs
- HTTPNotFoundHandler.cs
- ConvertEvent.cs
- PostBackOptions.cs
- ImageIndexConverter.cs
- SqlClientWrapperSmiStream.cs
- FixedSOMContainer.cs
- SetMemberBinder.cs
- MessageSecurityOverHttp.cs
- XslAst.cs
- XmlCustomFormatter.cs
- CodeExpressionStatement.cs
- DrawingBrush.cs
- FileUpload.cs
- CustomSignedXml.cs
- ToolStrip.cs
- BuilderInfo.cs
- DiscriminatorMap.cs
- Base64Stream.cs
- TypeSystemProvider.cs
- FileSystemInfo.cs
- XmlSchemaSimpleTypeUnion.cs
- FileClassifier.cs
- PipeConnection.cs
- ReaderWriterLock.cs
- SqlDataSourceFilteringEventArgs.cs
- StructuralCache.cs
- JournalEntry.cs
- TextShapeableCharacters.cs
- HTTPNotFoundHandler.cs
- BitmapMetadataBlob.cs
- PickBranch.cs
- KeyMatchBuilder.cs
- BindingsCollection.cs
- DispatcherEventArgs.cs
- SafeNativeMethodsMilCoreApi.cs
- Transactions.cs
- DependencyObjectPropertyDescriptor.cs
- QuerySettings.cs
- CompilerErrorCollection.cs
- XmlObjectSerializerReadContext.cs
- BufferedWebEventProvider.cs
- TextEditorThreadLocalStore.cs
- DataGridViewColumnTypeEditor.cs
- WebPartCatalogCloseVerb.cs
- CodeConditionStatement.cs
- EdmFunctionAttribute.cs
- TreeView.cs
- ProgressBarHighlightConverter.cs
- Int32EqualityComparer.cs
- XamlSerializerUtil.cs
- RuleSettings.cs
- SystemMulticastIPAddressInformation.cs
- SecurityUtils.cs
- XamlParser.cs
- Canvas.cs
- DeflateStream.cs
- DataAdapter.cs
- APCustomTypeDescriptor.cs
- XmlResolver.cs
- SqlTrackingWorkflowInstance.cs
- MenuEventArgs.cs
- TextSimpleMarkerProperties.cs
- TextTreeTextNode.cs
- SettingsBindableAttribute.cs
- DBCSCodePageEncoding.cs