Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Creating Secure Trial Versions for .NET Applications - A Tutorial

, 16 Oct 2012 CPOL
Implement trial licensing model for your .NET applications with minimal costs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SoftActivate.Licensing;

namespace SampleTrialAppCS
{
    public partial class ActivationForm : Form
    {
        public ActivationForm(string actionText, string licenseKey)
        {
            InitializeComponent();

            actionLabel.Text = actionText;
            this.licenseKey = licenseKey;
        }

        public void ActivateLicense()
        {
            isActivated = false;

            licensingClient = new LicensingClient("http://localhost:55355/",
                                                  CreateLicenseKeyTemplate(),  // no license key provided means we are requesting a trial version
                                                  licenseKey,
                                                  null,
                                                  12345 /* productId */
                                                 );

            licensingClient.OnLicensingException += new LicensingExceptionEventHandler(OnLicensingException);
            licensingClient.OnLicensingComplete += new LicensingCompleteEventHandler(OnLicensingComplete);

            // contact the licensing server and ask for a trial license for this computer's hardware id
            // if a trial license was already granted by the server for this particular computer and product id, the server will deny the request
            licensingClient.BeginAcquireLicense();

            ShowDialog();
        }

        public string ActivationKey
        {
            get
            {
                return activationKey;
            }
        }

        public string HardwareId
        {
            get
            {
                return hardwareId;
            }
        }

        public DateTime LicenseExpirationDate
        {
            get
            {
                return licenseExpirationDate;
            }
        }

        public bool IsActivated
        {
            get
            {
                return isActivated;
            }
        }

        public bool ClockManipulationDetected
        {
            get
            {
                return clockManipulationDetected;
            }
        }

        private void OnLicensingComplete()
        {
            isActivated = true;
            activationKey = licensingClient.ActivationKey;
            hardwareId = licensingClient.HardwareId;
            licenseExpirationDate = licensingClient.LicenseExpirationDate;
            clockManipulationDetected = KeyHelper.DetectClockManipulation(licenseExpirationDate);

            // This thread is a worker thread. We need to invoke the method on the UI thread.
            Invoke(new LicensingCompleteEventHandler(OnLicensingCompleteUI));
        }

        private void OnLicensingCompleteUI()
        {
            Close();
        }

        private void OnLicensingException(Exception ex)
        {
            isActivated = false;

            // This thread is a worker thread. We need to invoke the method on the UI thread.
            Invoke(new LicensingExceptionEventHandler(OnLicensingExceptionUI), ex);
        }

        private void OnLicensingExceptionUI(Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message);
            Close();
        }

        private KeyTemplate CreateLicenseKeyTemplate()
        {
            return new KeyTemplate(5, // number of groups of characters in the license key  
                                               5, // number of character in each group
                                               "doxzyMNu7n46whM=", // the public key used to verify the license key signature
                                               null, // private key is not needed for license key validation ! 
                                               109,  // signature size of the license key is 109 bits
                                               16,   // data size embedded in the license key is 16 bits
                                               "ProductId" // this is the name of the data field embedded in the license key
                                               );
        }

        LicensingClient licensingClient;
        string licenseKey;
        string hardwareId;
        string activationKey;
        bool isActivated;
        DateTime licenseExpirationDate;
        bool clockManipulationDetected;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ansel Reynard

United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 16 Oct 2012
Article Copyright 2012 by Ansel Reynard
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid