Microsoft 'NET Framework and Control Licensing Brian Roder Support Professional Developer Support Mi - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Microsoft 'NET Framework and Control Licensing Brian Roder Support Professional Developer Support Mi

Description:

License provider that is included with .NET Framework ... Create your own license provider. Inherit from abstract class LicenseProvider ... – PowerPoint PPT presentation

Number of Views:121
Avg rating:3.0/5.0
Slides: 30
Provided by: MicrosoftC
Category:

less

Transcript and Presenter's Notes

Title: Microsoft 'NET Framework and Control Licensing Brian Roder Support Professional Developer Support Mi


1
Microsoft .NET Framework and Control
LicensingBrian RoderSupport Professional
Developer Support Microsoft Corporation
2
Overview
  • Discuss design-time and run-time licensing
    support in the Microsoft .NET Framework

3
Session Prerequisites
  • Familiarity with the following
  • Microsoft Visual Basic .NET or Visual C .NET
    language syntax
  • Visual Studio .NET
  • .NET programming concepts
  • Class inheritance
  • Attributes
  • Basic understanding of object-oriented concepts

4
Agenda
  • Licensing overview
  • Creating a licensed class
  • LicFileLicenseProvider class
  • Using licensed classes
  • Creating custom licensed providers
  • Command-line support
  • QA

5
Licensing OverviewCOM Licensing
  • Originally, it was not built into COM
  • Support added with IClassFactory2
  • Burden is on the container application
  • Design-time and run-time support

6
Licensing Overview.NET Licensing
  • Built into the run time
  • License manager
  • License providers
  • Design time
  • Use a .lic file
  • Can be stored in any location
  • Run time
  • Embedded into EXE (Microsoft Windows
    applications)
  • Stored elsewhere (for Web applications)

7
Creating a Licensed Class
  • Apply the LicenseProviderAttribute
  • Visual Basic .NET
  • ltLicenseProvider(GetType(LicFileLicenseProvider))gt
    _
  • Public Class LicensedControl
  • End Class
  • Visual C .NET
  • LicenseProvider(typeof(CustomLicenseProvider))
  • public class LicensedControl System.Windows.Form
    s.UserControl
  • Declare a License field in the class
  • Visual Basic .NET
  • Private lic As License
  • Visual C .NET
  • private License lic

8
Creating a Licensed Class (2)
  • Fill the License in the class constructor
  • Visual Basic .NET
  • lic _
  • LicenseManager.Validate(GetType(LicensedContro
    l),Me)
  • Visual C .NET
  • lic
  • LicenseManager.Validate(typeof(LicensedControl
    ), this)
  • Release the License in class Dispose
  • Visual Basic .NET
  • lic.Dispose()
  • lic Nothing
  • Visual C .NET
  • lic.Dispose()
  • lic null

9
Visual Basic .NET Code Sample
  • Imports System.ComponentModel
  • ltLicenseProviderAttribute(GetType(LicFileLicensePr
    ovider))gt _
  • Public Class LicensedControl
  • Inherits System.Windows.Forms.UserControl
  • Private lic As License
  • Public Sub New()
  • MyBase.New()
  • lic LicenseManager.Validate(GetType(LicensedCont
    rol), Me)
  • End Sub
  • Protected Overloads Overrides Sub Dispose( _
  • ByVal
    disposing As Boolean)
  • lic.Dispose()
  • lic Nothing
  • MyBase.Dispose(disposing)
  • End Sub

10
Visual C .NET Code Sample
using System.ComponentModel using
System.Windows.Forms LicenseProviderAttribute(t
ypeof(LicFileLicenseProvider)) public class
LicensedControl2 UserControl private
License lic public LicensedControl2() lic
LicenseManager.Validate(typeof(LicensedControl),
this)
protected override void Dispose(bool disposing)
lic.Dispose() lic null base.Dispose(dispo
sing)
11
LicFileLicenseProvider Class
  • License provider that is included with .NET
    Framework
  • Similar validation scheme to IClassFactory2
  • Design time
  • License files are used (.lic)
  • Run time
  • Run-time licenses embedded into EXE

12
.lic File
  • Provides design-time licensing support
  • Text file with a single text string
  • ltNamespacegt.ltClassNamegt is a licensed component.
  • File naming convention
  • ltNamespacegt.ltClassNamegt.LIC
  • Located in the same directory as the built
    assembly

13
Using a Licensed Class
  • Object creation is the same
  • Visual Basic .NET
  • Dim aLicensedClass As New LicensedClass()
  • Visual C .NET
  • LicensedClass aLicensedClass new
    LicensedClass()
  • Licenses embedded in the EXE as resource
  • Licenses.licx used by Visual Studio .NET
  • At run time
  • Retrieve embedded license
  • or
  • .LIC file (LicFileLicenseProvider)

14
Licenses.licx
  • Used by Visual Studio .NET
  • Licensed class on each line
  • Format
  • ltNamespacegt.ltClassnamegt, ltAssembly Namegt
  • Example
  • Licensing.LicensedClass, Licensing
  • Forms designers maintain this automatically
  • Classes without a designer
  • Manual file modification is required
  • Licenses embedded into EXE at build time

15
Custom License Providers
  • LicFileLicenseProvider is an example
  • Easy to use
  • Built into the run time
  • Similar to IClassFactory2
  • Not secure
  • Create your own license provider
  • Inherit from abstract class LicenseProvider
  • Override GetLicense method
  • Create a custom License class
  • Specify new provider in LicenseProviderAttribute

16
Custom License Providers (2)
  • GetLicense
  • Create and return a valid license
  • Context object for design time vs. run time
  • Set or retrieve embedded license key
  • IsKeyValid
  • Called in GetLicense
  • Optional used to separate validation logic
  • Inheriting from License class
  • Create custom properties
  • Enable/disable features

17
Visual Basic .NET Code Sample
  • Public Overrides Function GetLicense(ByVal
    context ) as License
  • Dim lic As RegLicense
  • Dim key As String
  • If context.UsageMode LicenseUsageMode.Design
    time Then
  • key GetRegKey()
  • If key ltgt "" And IsKeyValid(key, type) Then
  • lic New RegLicense(Me, key)
  • context.SetSavedLicenseKey(type,
    lic.LicenseKey)
  • End If
  • ElseIf context.UsageMode LicenseUsageMode.Ru
    ntime Then
  • key context.GetSavedLicenseKey(type, Nothing)
  • If (key ltgt Nothing And IsKeyValid(key,
    type)) Then
  • lic New RegLicense(Me, key)
  • End If
  • End If
  • Return lic
  • End Function

18
Visual Basic .NET Code SampleIsKeyValid Function
  • Protected Function IsKeyValid(ByVal key As
    String, _
  • ByVal type As
    System.Type) _
  • As Boolean
  • If key "Full" Or key "Restricted" Then
  • Return True
  • Else
  • Return False
  • End If
  • End Function

19
Visual Basic .NET Code SampleGetRegKey Function
  • Function GetRegKey() As String
  • Dim regkey As RegistryKey
  • Dim keyvalue As String
  • regkey Registry.LocalMachine.OpenSubKey( _

  • "SOFTWARE\SomeCompany\License", _
  • False)
  • If Not regkey Is Nothing Then
  • keyvalue CStr(regkey.GetValue(""))
  • regkey.Close()
  • Return keyvalue
  • Else
  • Return ""
  • End If
  • End Function

20
Visual C .NET Code SampleGetLicense Function
  • public override License GetLicense(LicenseContext
    context )
  • RegLicense lic null
  • if(context.UsageMode LicenseUsageMode.Designt
    ime)
  • string key GetRegKey()
  • if(key ! null IsKeyValid(key, type))
  • lic new RegLicense(this, key)
  • context.SetSavedLicenseKey(type,
    lic.LicenseKey)
  • else if(context.UsageMode LicenseUsageMode.Ru
    ntime)
  • string key context.GetSavedLicenseKey(type,
    null)
  • if(key ! null IsKeyValid(key, type))
  • lic new RegLicense(this, key)
  • return lic

21
Visual C .NET Code SampleIsKeyValid Function
  • protected bool IsKeyValid(string key, System.Type
    type)
  • if(key "Full" key "Restricted")
  • return true
  • else
  • return false

22
Visual C .NET Code SampleGetRegKey Function
  • public string GetRegKey()
  • RegistryKey regkey
  • string keyvalue
  • regkey Registry.LocalMachine.OpenSubKey(
  • "SOFTWARE\\SomeCompany\\License",
  • false)
  • if(regkey ! null)
  • keyvalue regkey.GetValue("").ToString()
  • regkey.Close()
  • return keyvalue
  • else
  • return null

23
Using a Custom License ClassVisual Basic .NET
Sample
  • Public Sub New()
  • MyBase.New()
  • lic CType(LicenseManager.Validate(GetType(Lice
    nsedControl) _
  • , Me),
    RegLicense)
  • Select Case lic.Access
  • Case LicenseTypeEnum.Full
  • 'Enable/Disable Features
  • Case LicenseTypeEnum.Restricted
  • 'Enable/Disable Features
  • End Select
  • End Sub

24
Using a Custom License ClassVisual C .NET Sample
  • public LicensedControl()
  • lic (RegLicense) LicenseManager.Validate(
  • typeof(LicensedControl),
  • this)
  • switch(lic.Access)
  • case LicenseTypeEnum.Full
  • //Enable/Disable Features
  • break
  • case LicenseTypeEnum.Restricted
  • //Enable/Disable Features
  • break

25
Command-Line SupportLicensing Compiler
  • Lc.exe
  • Licensing compiler
  • Generates .licenses file
  • Target name is embedded
  • Format
  • lc /targetltLicense Filegt /complistltlicx filegt
    /iltassemblynamegt
  • Example
  • lc /targetWindowsApp.exe /complist../licenses.li
    cx /ilicensing.dll

26
Command-Line Support.NET Language Compilers
  • Embed license as a resource in target EXE
  • Compiler switch
  • Example
  • Visual Basic .NET
  • vbc form1.vb /targetexe
    /resourcewindowsapp.exe.licenses
  • /outwindowsapp.exe
  • Visual C .NET
  • csc /outwindowsapp.exe /rlicensing.dll
    /resourcewindowsapp.exe.licenses form1.cs

27
Special Considerations
  • Web Forms controls
  • Where is the assembly running from?
  • Internet connectivity permits advanced licensing
    schemes
  • Windows Forms controls in Internet Explorer
  • ltLINK REL"licenses" HREF"license.htm.licenses"gt
  • ltOBJECT id ctrl HEIGHT"400" WIDTH"400"
    classid"httpLicensing.dllLicensing.LicensedCont
    rol"gt
  • lt/OBJECT gt

28
Summary
  • Microsoft .NET Framework built with licensing in
    mind
  • Flexible and straightforward
  • Consistent model across application types
  • Visual Studio .NET support
  • Licenses.licx files
  • Automatic generation of .licenses file
  • Lc.exe provides command-line support

29
  • Thank you for joining us for Todays Microsoft
    Support
  • WebCast.
  • For information on all upcoming Support WebCasts
    and
  • access to the archived content (streaming media
    files,
  • PowerPoint slides, and transcripts), please
    visit
  • http//support.microsoft.com/WebCasts
  • We sincerely appreciate your feedback. Please
    send any
  • comments or suggestions regarding the Support
  • WebCasts to supweb_at_microsoft.com
Write a Comment
User Comments (0)
About PowerShow.com