September 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Applications Licensing using the .NET Framework

  • September 8, 2003
  • By Kenn Scribner
  • Send Email »
  • More Articles »

Kenn Scribner is with Wintellect

If you're reading this, you're undoubtedly a Windows software developer. And if you're a Windows software developer, you probably write software because you're passionate about, well, writing software. I'd happily code all day long just because I enjoy it, and I'm sure many of you feel the same way. Could there be a better way to make a living?

Ah, but that's the catch. If you stop to consider the financial aspects of writing software, the plain truth is that while we may do this work because we enjoy it, we all have families to feed, bills to pay, and frankly we all like to go on holiday from time to time. It's not an unfair observation, after all, and people using our software probably feel the same way about their careers. They work and want to be compensated for their efforts, even if secretly they love what they're doing and would work at it anyway.

The problem is that software is often a nebulous thing when given to an end user. All they see is the application itself, and even then it only exists for a moment in time as electrical charges in their computer's memory. That hardly seems like a product that took hundreds or thousands of hours to develop and test, or at least is not physically comparable to objects such as a car or a refrigerator. Honest, hardworking computer users that would never consider stealing a car or a refrigerator often think nothing of trading software with friends, and oftentimes they even know this is against the licensing agreements that came with the software. But the software is just bits on a CD, right? What harm can it be to hand that software to their brother or friend?

Well, you know the answer to that! That's one user that didn't pay you for your hard work. In business terms you lost revenue. If you lose enough revenue, as a business you go out of business. As an individual, you can't meet your financial obligations (all those bills you pay), so you either work at a fast food chain to make ends meet or you declare bankruptcy and live in a cardboard box under a freeway overpass. Personally, I'd rather make sure that user pays for the application and ultimately my hard work to design, develop, test, market, and distribute the software I wrote.

The solution is software licensing. As an industry, many people have tried to solve this problem in various ways. In the past software was sold on copy-proof media (remember the floppy disks with the extra track?). These days you often have to enter some sort of software key to enable crippled software packages. Microsoft recently began online Internet-based software authorization with their high-end operating systems and products (such as Office, probably the most often pirated piece of software ever written). Within the .NET Framework, there also exist the nuggets of technology you can use to license your applications, and that's what I'd like to show you now.

Control Licensing

Nearly everything you'll read about .NET licensing binds the licensing concept to controls, with the idea being control developers could ship controls that are licensed at design time and/or run time. As it happens, you can apply Framework licensing to any class derived from System.Windows.Forms.Control, which would include entire Windows Forms applications, but I'll begin with controls themselves. The basic control licensing UML static class diagram is shown in Figure 1.

Figure 1. .NET Control Licensing Static Class Diagram.

The general execution sequence is shown (as a UML sequence diagram) in Figure 2. The licensed control, in its constructor, requests a license from the LicenseManager:

license = LicenseManager.Validate(typeof(MyLicensedControl), this);

In this case, the constructor is for the licensed control as implemented in the class MyLicensedControl. Interestingly, there may be nothing more you need to do with the license object itself (it depends upon the implementation of the license), except to properly dispose of it as there may be resources attached. Out of the box, the important action we took was to call the license manager and ask for a license. If a license is not to be granted for some reason, the Validate() call will fail with an exception if exceptions are desired or return a null license if exceptions are to be suppressed. (The call to LicenseProvider.GetLicense() controls this, and the default Framework implementation is to allow exceptions.)

Figure 2. Control Licensing Sequence.,/p>

The license manager, in turn, calls the control's license provider's GetLicense() method. And how does the license manager, a .NET Framework component, know which license provider is to be used? After all, you provide the license provider... The answer is through metadata in the traditional .NET way, via attribute:

[LicenseProvider(typeof(MyLicenseProvider))]
public class MyLicensedControl : System.Windows.Forms.Control
{
   ...
}

The license manager looks for the LicenseProvider attribute attached to the licensed control class. Since the LicenseProvider attribute takes the type of the license provider as input to its constructor, the license manager can reflect this information when asked to validate the license. Your license provider must be derived from the LicenseProvider class and must override GetLicense(), an abstract method (MustOverride for the Visual Basic fans):

public class MyLicenseProvider : 
                System.ComponentModel.LicenseProvider
{
   ...

   public override License GetLicense(
      LicenseContext context, 
      Type type, 
      object instance, 
      bool allowExceptions) 
   {
      ...
   }
}

The magic within .NET that allows for licensing consists of the process I just described. But in the end, the real licensing magic is encoded within the GetLicense() method. Before we get too imaginative ourselves, you should know that Microsoft ships with the Framework a rudimentary license provider called LicFileLicenseProvider. While I believe that for completeness I should describe how you use LicFileLicenseProvider, I also believe that anyone serious about licensing their software will most likely avoid such a simple scheme in favor of their own, more creative schemes, one of which I'll introduce to you after describing LicFileLicenseProvider.





Page 1 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel