Before you deploy a Web application to a production server, you may need to add licensing code to protect against the unauthorized usage of your application. With Babel Licensing is very easy to add licenses in a Web application. In this article we will show in a few steps, how to check whether your Web application is running on a licensed server or not.

The sample code is written in ASP.NET MVC using Visual Studio 2013. Don't worry if you have a Web Form application, there are few steps to follow and the migration to Web Form should be easy.

Reference Babel Licensing

The first thing you need to do, is to add a reference to the Babel.Licensing.dll library in your project.
Right click the References node in the Solution Explorer and add Babel.Licensing from the Extensions assemblies.

Add License Check

Now we have to add the code that performs the check of the license file. You have many options here, for the sake of simplicity we choose to check the license in the Home controller when the user loads the Index page. We created in the Index action a LicenseModel class that we use in the Razor view to show the license information.


    public class HomeController : Controller

    {

        public ActionResult Index()

        {

            var model = new LicenseModel();

            model.ThisHardwareId = new HardwareId(HardwareComponents.ComputerName | HardwareComponents.Cpu).ToMachineKey();

 

            try

            {

                XmlLicense license = LicenseManager.Validate(typeof(HomeController), this) as XmlLicense;

                model.Licensee = license.Licensee.Company;

                model.ExpireDate = license.ExpireDate;


                HardwareRestriction restriction = license.Restrictions.OfType<HardwareRestriction>().FirstOrDefault();

                if (restriction != null)

                    model.LicenseHardwareKey = restriction.HardwareKey;

            }

            catch (LicenseException e)

            {

                model.Error = e.Message;

            }

            return View(model);

        }


License validation is performed by the LicenseManager call to Validate method. Here we need to pass the type of our HomeController and the object instance. These two parameters will be used by the license engine to search for the proper license file and perform integrity check of our XML license.

If the license validation passes, we get an XmlLicense object from the Validate call otherwise an exception is raised. In the latter case we have to catch the exception and let the user know that the validation has failed.

OK. Now that the validation code is in place there are two other things left to do. First we need to add the LicenseProviderAttribute to the HomeController class. This to plug into the license manager the FileLicenseProvider that will be used to perform search and validation.

    [LicenseProvider(typeof(FileLicenseProvider))]

    public class HomeController : Controller

    {

Then we need to specify the path where to search for license files. Open the Global.asax.cs file and add the following code to Application_Start() method:

     protected void Application_Start()

    {

        // Set search path for license files

        FileLicenseProvider.SearchDirectories = new string[] { Server.MapPath("~/Licenses") };


That's it! Now press F5 to run the application and see what we get.

License validation failed

The Validate method has not passed because we haven't generated a license yet. Let's generate one for our Web application.

Generate the License File

In the Index page we have put the hardware key of the machine where the server is running. To generate this key, we used the Computer name and the CPU serial number. You can use other hardware sources like Motherboard serial number or Ethernet card MAC address. Please read the Babel Licensing user's guide to know more about this topic. 

Copy the lic.exe command line and open a Babel Command Prompt.
Cd into the Web application bin folder and paste the command line for lic.exe:

lic.exe WebLicensed.dll --sign --keyfile "..\Keys.snk" --out "..\Licenses\WebLicensed.licenses" --hardware
C84CK-BDNDX-WTAF8-RHBIX-17NQE --licensee company="ACME S.p.a."

The license file WebLicensed.licenses will be generated into the Licenses folder.

Now refresh the Index page by pressing F5 again, and you will see your Web application licensed.

I have prepared a Visual Studio solution with the Web application code described in this article.
If you have any question about this topic feel free to contact us at support@babelfor.net

Back to List