NET User Interfaces in Csharp Windows Forms and Custom Controls [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

NET User Interfaces in Csharp Windows Forms and Custom Controls [Electronic resources] - نسخه متنی

Matthew MacDonald

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید




































Licensing Custom Controls



Licensing in the .NET world is far more customizable and far less painful than it was with ActiveX controls. The .NET framework provides several licensing classes in the System.ComponentModel namespace. By using and extending these classes, you can grant or allow access to your control, using ordinary .NET code to check external resources like the Windows registry, an XML file, or even a remote web service for registration information.



Simple LIC File Licensing



To best understand .NET licensing, it helps to start with a simple example using the LicFileLicenseProvider class. This class doesn't provide any real protection, but it's a stepping-stone to the more advanced licensing strategies you look at next.


The LicFileLicenseProvider searches for a text file in the same directory as the control assembly. This LIC file uses the control's fully qualified class name for a filename, so the DirectoryTree control requires a license file named DirectoryTreeControl.DirectoryTree.LIC. Inside this file is a simple predefined text string in the format "[Component] is a licensed component."


Thus, the contents of the DirectoryTreeControl.DirectoryTree.LIC file would be:



DirectoryTreeControl.DirectoryTree is a licensed component.


This file must be placed in the client project's bin directory (where Visual Studio .NET compiles the final exe just prior to launching it).



Note


It's worth noting that these LIC files don't need to be distributed with a client application.When you compile a Windows program, a license.licx file is created with all the licensing information for all license controls. This file is compiled as a binary resource and embedded in the final client assembly. However, if another developer wants to create a client application with your control, a LIC source file is needed.To enforce LIC file licensing, you need to add a LicenseProvider attribute to your control class that tells .NET to use the LicFileProvider class to validate licenses.



[LicenseProvider(typeof(LicFileLicenseProvider))]
public class DirectoryTree : TreeView


Additionally, you need to create the license when the control is created, using the static Validate() method of the LicenseManager Help class:



private License license;
public DirectoryTree()
{
license = LicenseManager.Validate(this.GetType(), this);
}


The Validate() method throws a LicenseException if it doesn't find the correct string in the LIC file, and refuses to create your control. This restriction applies both to design time and runtime control creation.


Finally, you need to dispose of the license when the control is disposed.



protected override void Dispose(bool disposing)
{
if (license != null)
{
license.Dispose();
}
base.Dispose(disposing);
}




Custom LIC File Licensing



Clearly, simple LIC file licensing doesn't offer much in the way of protection. Any user who knows a little about the .NET framework will realize the generic format that must be created for a LIC file. However, you can add more stringent requirements by creating a custom license provider based on the LicFileLicenseProvider.


All you need to do is inherit from the class and override the IsValid() method. The IsValid() method receives the contents of the LIC file, and returns true or false to indicate if the contents are correct. Thus, you could use the IsValid() method to check a license number against a company-specific algorithm.


The example below extracts the first three characters from the license file, and verifies that they correspond to a number that is divisible by 7.



public class FileLicenseProvider : LicFileLicenseProvider
{
protected override bool IsKeyValid(string key, System.Type type)
{
int code = int.Parse(key.Substring(0, 3))
if (code <> 0)
{
if (Math.IEEERemainder(Code, 7) == 0)
{
return true;
else
{
return false;
}
}
else
{
return false;
}
}
}





Advanced License Providers



Control licensing doesn't need to be based on LIC files. In fact, you can create any type of licensing scheme imaginable. You can even perform tremendously annoying tricks like only allowing controls to be registered to specific computers. To implement a custom licensing scheme, you need to create two classes: a custom license provider, and a custom license.


The custom license is the easiest ingredient. It simply derives from the base License class, overrides the LicenseKey property and Dispose() method, and adds properties for any required pieces of information. You also need to add a constructor that configures the license, as the LicenseKey property is read-only.



public class CustomLicense : License
{
private string key;
public override string LicenseKey
{
get
{
return key;
}
}
public CustomLicense(string key)
{
this.key = key;
}
public override void Dispose()
{
// This method must be overriden.
}
}


The custom LicenseProvider plays the same role as the LicFileLicenseProvider. It provides a GetLicense() method, which the .NET framework calls to validate the control. For example, when you use the LicenseManager.Validate() method in the constructor for the DirectoryTree control, .NET uses the LicenseProvider.GetLicense() method to retrieve the license.


In the GetLicense() method, you may want to examine whether the component is in design-time or runtime mode, and apply different rules. Additionally, you may want to return a valid license object, nothing at all, or throw a LicenseException to indicate that the control should not be created. The LicFileProvider throws a LicenseException to indicate when a LIC file is not valid.


The example that follows looks for a predefined registry entry at design time. At runtime, it first examines the current context, and then defaults to the registry if a compiled license key can't be found. The registry value is stored under a predefined company name, followed by the fully qualified name of the control. The key is validated as long as it matches the string "1234567890" and a CustomLicense object encapsulating this key is returned.



public class RegistryLicenseProvider : LicenseProvider
{
public override System.ComponentModel.License GetLicense(
System.ComponentModel.LicenseContext context,
System.Type type, object instance, bool allowExceptions)
{
string key;
if (context.UsageMode == LicenseUsageMode.Runtime)
{
// Try to find key in current context.
key = context.GetSavedLicenseKey(type, null);
}
// Always look in the registry at design time.
// If the key wasn't found in the current context at runtime,
// we can also look in the registry.
// Another option might be to always allow the control at runtime,
// and just restrict it at design time.
if (key == ")
{
// A debugging hint (remove when you perfect the licensing scheme):
MessageBox.Show("Performing registry lookup.",
"RegistryLicenseProvider");
RegistryKey rk;
rk = Registry.LocalMachine.OpenSubKey(@"Software\MyCompany\" +
type.ToString());
if (rk != null)
{
key = rk.GetValue("LicenseKey", ");
}
// Save key in current context.
if (key != ")
{
context.SetSavedLicenseKey(type, key);
}
}
// Check if key is valid.
if (!IsValid(key))
{
if (!allowExceptions)
{
throw new LicenseException(type);
}
}
// Return the license object.
return new CustomLicense(key);
}
private bool IsValid(string key)
{
if (key == "1234567890")
{
return true;
}
else
{
return false;
}
}
}


The GetLicense() method is provided with a fair bit of information, including the current LicenseContext, the type of the component that is requesting the license, and a reference to instance of the component. This means you can easily create a single LicenseProvider that could handle the licensing for all different types of controls. Custom licensing schemes are limited only by your imagination, and can become quite complex. The material presented here is only a basic introduction for what a component vendor might do.






/ 142