Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / CompMod / System / ComponentModel / LicFileLicenseProvider.cs / 1305376 / LicFileLicenseProvider.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.ComponentModel { using System; using System.ComponentModel.Design; using System.Diagnostics; using System.Globalization; using System.IO; using System.Reflection; using System.Runtime.Remoting; using System.Security.Permissions; ////// /// [HostProtection(SharedState = true)] public class LicFileLicenseProvider : LicenseProvider { ///Provides an implementation of a ///. The provider works in /// a similar fashion to Microsoft .NET Framework standard licensing module. /// /// protected virtual bool IsKeyValid(string key, Type type) { if (key != null) { return key.StartsWith(GetKey(type)); } return false; } ///Determines if the key retrieved by the ///method is valid /// for the specified type. /// /// Creates a key for the specified type. /// protected virtual string GetKey(Type type) { // This string should not be localized. // return string.Format(CultureInfo.InvariantCulture, "{0} is a licensed component.", type.FullName); } ////// /// public override License GetLicense(LicenseContext context, Type type, object instance, bool allowExceptions) { LicFileLicense lic = null; Debug.Assert(context != null, "No context provided!"); if (context != null) { if (context.UsageMode == LicenseUsageMode.Runtime) { string key = context.GetSavedLicenseKey(type, null); if (key != null && IsKeyValid(key, type)) { lic = new LicFileLicense(this, key); } } if (lic == null) { string modulePath = null; if (context != null) { ITypeResolutionService resolver = (ITypeResolutionService)context.GetService(typeof(ITypeResolutionService)); if (resolver != null) { modulePath = resolver.GetPathOfAssembly(type.Assembly.GetName()); } } if (modulePath == null) { modulePath = type.Module.FullyQualifiedName; } string moduleDir = Path.GetDirectoryName(modulePath); string licenseFile = moduleDir + "\\" + type.FullName + ".lic"; Debug.WriteLine("Looking for license in: " + licenseFile); if (File.Exists(licenseFile)) { Stream licStream = new FileStream(licenseFile, FileMode.Open, FileAccess.Read, FileShare.Read); StreamReader sr = new StreamReader(licStream); string s = sr.ReadLine(); sr.Close(); if (IsKeyValid(s, type)) { lic = new LicFileLicense(this, GetKey(type)); } } if (lic != null) { context.SetSavedLicenseKey(type, lic.LicenseKey); } } } return lic; } private class LicFileLicense : License { private LicFileLicenseProvider owner; private string key; public LicFileLicense(LicFileLicenseProvider owner, string key) { this.owner = owner; this.key = key; } public override string LicenseKey { get { return key; } } public override void Dispose() { GC.SuppressFinalize(this); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ //Gets a license for the instance of the component and determines if it is valid. ///// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.ComponentModel { using System; using System.ComponentModel.Design; using System.Diagnostics; using System.Globalization; using System.IO; using System.Reflection; using System.Runtime.Remoting; using System.Security.Permissions; ////// /// [HostProtection(SharedState = true)] public class LicFileLicenseProvider : LicenseProvider { ///Provides an implementation of a ///. The provider works in /// a similar fashion to Microsoft .NET Framework standard licensing module. /// /// protected virtual bool IsKeyValid(string key, Type type) { if (key != null) { return key.StartsWith(GetKey(type)); } return false; } ///Determines if the key retrieved by the ///method is valid /// for the specified type. /// /// Creates a key for the specified type. /// protected virtual string GetKey(Type type) { // This string should not be localized. // return string.Format(CultureInfo.InvariantCulture, "{0} is a licensed component.", type.FullName); } ////// /// public override License GetLicense(LicenseContext context, Type type, object instance, bool allowExceptions) { LicFileLicense lic = null; Debug.Assert(context != null, "No context provided!"); if (context != null) { if (context.UsageMode == LicenseUsageMode.Runtime) { string key = context.GetSavedLicenseKey(type, null); if (key != null && IsKeyValid(key, type)) { lic = new LicFileLicense(this, key); } } if (lic == null) { string modulePath = null; if (context != null) { ITypeResolutionService resolver = (ITypeResolutionService)context.GetService(typeof(ITypeResolutionService)); if (resolver != null) { modulePath = resolver.GetPathOfAssembly(type.Assembly.GetName()); } } if (modulePath == null) { modulePath = type.Module.FullyQualifiedName; } string moduleDir = Path.GetDirectoryName(modulePath); string licenseFile = moduleDir + "\\" + type.FullName + ".lic"; Debug.WriteLine("Looking for license in: " + licenseFile); if (File.Exists(licenseFile)) { Stream licStream = new FileStream(licenseFile, FileMode.Open, FileAccess.Read, FileShare.Read); StreamReader sr = new StreamReader(licStream); string s = sr.ReadLine(); sr.Close(); if (IsKeyValid(s, type)) { lic = new LicFileLicense(this, GetKey(type)); } } if (lic != null) { context.SetSavedLicenseKey(type, lic.LicenseKey); } } } return lic; } private class LicFileLicense : License { private LicFileLicenseProvider owner; private string key; public LicFileLicense(LicFileLicenseProvider owner, string key) { this.owner = owner; this.key = key; } public override string LicenseKey { get { return key; } } public override void Dispose() { GC.SuppressFinalize(this); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.Gets a license for the instance of the component and determines if it is valid. ///
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- LicFileLicenseProvider.cs
- UrlMappingsSection.cs
- DataServiceHostFactory.cs
- QuaternionValueSerializer.cs
- BinarySerializer.cs
- AdornerPresentationContext.cs
- Pkcs9Attribute.cs
- TypeBuilder.cs
- SoapEnumAttribute.cs
- NonSerializedAttribute.cs
- UnmanagedHandle.cs
- DelegatingTypeDescriptionProvider.cs
- CqlWriter.cs
- ScrollBar.cs
- Int64Storage.cs
- Behavior.cs
- WebResourceUtil.cs
- PeerNameRecordCollection.cs
- ConstructorBuilder.cs
- _LocalDataStoreMgr.cs
- Utils.cs
- FixedSOMTable.cs
- ErrorLog.cs
- DrawingContextWalker.cs
- TrackBarDesigner.cs
- TextParagraph.cs
- Ref.cs
- AddInServer.cs
- SqlLiftIndependentRowExpressions.cs
- AttachedAnnotation.cs
- CachedTypeface.cs
- ControlParser.cs
- XmlNamespaceManager.cs
- AsymmetricSignatureDeformatter.cs
- TextParaClient.cs
- TemplateControlBuildProvider.cs
- Misc.cs
- InfoCardRSAOAEPKeyExchangeDeformatter.cs
- DrawingCollection.cs
- StrongNameIdentityPermission.cs
- ExpressionReplacer.cs
- IPPacketInformation.cs
- SqlParameterizer.cs
- CryptoApi.cs
- CompatibleIComparer.cs
- DataGridViewBand.cs
- TemplateBamlRecordReader.cs
- StorageComplexPropertyMapping.cs
- TextSelectionHighlightLayer.cs
- PreloadHost.cs
- BrowserCapabilitiesCompiler.cs
- UserValidatedEventArgs.cs
- LoaderAllocator.cs
- DrawingBrush.cs
- ListViewPagedDataSource.cs
- ErrorFormatterPage.cs
- XmlILAnnotation.cs
- HwndHost.cs
- CustomAttributeFormatException.cs
- DataGridViewHitTestInfo.cs
- DataControlFieldCollection.cs
- FileUtil.cs
- TextDocumentView.cs
- FixedPage.cs
- InheritanceAttribute.cs
- FilterElement.cs
- LightweightCodeGenerator.cs
- TextInfo.cs
- UpDownBase.cs
- IxmlLineInfo.cs
- IconHelper.cs
- ContainsRowNumberChecker.cs
- TemplateBaseAction.cs
- PointAnimationUsingPath.cs
- TakeOrSkipWhileQueryOperator.cs
- CompilerState.cs
- UnsafeNativeMethods.cs
- InkCanvasFeedbackAdorner.cs
- ListView.cs
- DataListCommandEventArgs.cs
- WindowsTreeView.cs
- ColumnMapCopier.cs
- SqlTypeSystemProvider.cs
- DataGridColumnHeadersPresenterAutomationPeer.cs
- JavaScriptString.cs
- MsmqException.cs
- EllipseGeometry.cs
- PathSegment.cs
- SoapSchemaExporter.cs
- ProtocolState.cs
- CustomAttributeFormatException.cs
- TextTreeTextBlock.cs
- CodeCatchClause.cs
- DesignerValidatorAdapter.cs
- RelativeSource.cs
- SemanticResolver.cs
- BlurBitmapEffect.cs
- WebPartMenuStyle.cs
- CompoundFileReference.cs
- Win32.cs