Licensing in .NET
By Myk Willis, October 01, 2003
The Microsoft.NET licensing model works well for the kind of redistributable components that developers buy and redistribute as a part of an application. In this article, we'll enable licensing support for a simple redistributable component, and see how Visual Studio .NET automates much of the licensing process for components.
Licensing in .NET
Listing 6 A license provider with support for evaluation license keys
using System;
using System.ComponentModel;
using Microsoft.Win32;
public class EvaluationLicenseProvider : CustomLicenseProvider
{
protected override string GetDesignTimeLicenseKey(Type type)
{
RegistryKey regKey = Registry.LocalMachine.OpenSubKey(
"Software\\WdmLicensing\\" + type.FullName );
if ( regKey == null )
{
return null;
}
return regKey.GetValue( "LicenseKey" ).ToString();
}
protected override bool IsLicenseKeyValid(Type type, string licenseKey)
{
return ( licenseKey.Equals( GetValidLicenseKey(type) ) ||
licenseKey.Equals("Evaluation") );
}
private string GetValidLicenseKey(Type type)
{
int hashCode = (type.FullName + "Secret").GetHashCode();
return String.Format( "{0}:{1,8:X}", type.FullName, hashCode );
}
protected override License CreateLicense(Type type, string licenseKey)
{
return new CustomLicense(licenseKey, licenseKey.Equals("Evaluation"));
}
}