Click here to Skip to main content
13,297,799 members (45,708 online)
Click here to Skip to main content


233 bookmarked
Posted 9 Oct 2012

Creating Secure Trial Versions for .NET Applications - A Tutorial

, 16 Oct 2012
Implement trial licensing model for your .NET applications with minimal costs
´╗┐Imports System.Threading
Imports SoftActivate.Licensing

Class MainForm

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        settings = New AppSettings()

    End Sub

    Private Sub BeginLicenseValidation()
        Dim _licenseValidationThread As Thread = New Thread(New ParameterizedThreadStart(AddressOf LicenseValidationThread))
    End Sub

    Private Delegate Sub SetTextBoxValueDelegate(textBox As TextBox, value As String)

    Sub SetTextBoxValue(textBox As TextBox, value As String)
        textBox.Text = value
    End Sub

    Private Sub LicenseValidationThread(param As Object)

        Dim registrationStatus As String = "NOT REGISTERED"
        Dim clockManipulationStatus As String = "NOT DETECTED"
        Dim isLicenseValid As Boolean = False
        Dim licenseExpirationDate As System.DateTime = System.DateTime.MinValue

        REM if a license key is not saved in the product settings, ask the user to enter the license key
        If (Not (String.IsNullOrEmpty(settings.GetProperty("ActivationKey")) Or String.IsNullOrEmpty(settings.GetProperty("ActivationHardwareId")) Or String.IsNullOrEmpty(settings.GetProperty("LicenseKey")))) Then
            Dim licensingClient As LicensingClient = New LicensingClient(CreateLicenseKeyTemplate(), settings.GetProperty("LicenseKey"), Nothing, settings.GetProperty("ActivationHardwareId"), settings.GetProperty("ActivationKey"))

            If licensingClient.IsLicenseValid() Then
                isLicenseValid = True
                licenseExpirationDate = licensingClient.LicenseExpirationDate

                If settings.GetProperty("LicenseKey") = trialLicenseKey Then
                    registrationStatus = "TRIAL" + " (" + (1 + (licenseExpirationDate - DateTime.UtcNow).Days).ToString() + " days remaining)"
                    registrationStatus = "REGISTERED"
                End If
                registrationStatus = "INVALID LICENSE "

                Select Case licensingClient.LicenseStatus
                    Case LICENSE_STATUS.Expired
                        registrationStatus = registrationStatus + "(expired on " + licensingClient.LicenseExpirationDate.Month + "/" + licensingClient.LicenseExpirationDate.Day + "/" + licensingClient.LicenseExpirationDate.Year + ")"
                    Case LICENSE_STATUS.InvalidActivationKey
                        registrationStatus = registrationStatus + "(invalid activation key)"
                    Case LICENSE_STATUS.InvalidHardwareId
                        registrationStatus = registrationStatus + "(invalid hardware id)"
                    Case Else
                        registrationStatus = registrationStatus + "(unknown reason)"
                End Select
            End If
        End If

        Invoke(New SetTextBoxValueDelegate(AddressOf SetTextBoxValue), New Object() {txtRegistrationStatus, registrationStatus})
        Invoke(New SetTextBoxValueDelegate(AddressOf SetTextBoxValue), New Object() {txtLicenseKey, settings.GetProperty("LicenseKey")})
        Invoke(New SetTextBoxValueDelegate(AddressOf SetTextBoxValue), New Object() {txtActivationHardwareId, settings.GetProperty("ActivationHardwareId")})
        Invoke(New SetTextBoxValueDelegate(AddressOf SetTextBoxValue), New Object() {txtActivationKey, settings.GetProperty("ActivationKey")})

        If isLicenseValid Then
            If ClockManipulationDetector.DetectClockManipulation(licenseExpirationDate) Then
                clockManipulationStatus = "CLOCK MANIPULATION DETECTED !"
            End If
        End If

        Invoke(New SetTextBoxValueDelegate(AddressOf SetTextBoxValue), New Object() {txtClockManipulationStatus, clockManipulationStatus})
    End Sub

    Private Sub MainForm_Closing(sender As Object, args As FormClosingEventArgs) Handles MyBase.FormClosing
    End Sub

    Private Sub btnStartTrial_Click(sender As System.Object, e As System.EventArgs) Handles btnStartTrial.Click
        Dim activationForm As ActivationForm = New ActivationForm("Acquiring trial license...", trialLicenseKey)

        If activationForm.IsActivated Then
            settings.SetProperty("LicenseKey", trialLicenseKey)
            settings.SetProperty("ActivationKey", activationForm.ActivationKey)
            settings.SetProperty("ActivationHardwareId", activationForm.HardwareId)

        End If
    End Sub

    Private Sub btnDeleteRegistrationData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteRegistrationData.Click
        settings.SetProperty("LicenseKey", "")
        settings.SetProperty("ActivationKey", "")
        settings.SetProperty("ActivationHardwareId", "")


        txtRegistrationStatus.Text = "NOT REGISTERED"

        MessageBox.Show("Registration data was deleted for the sample application, but in order to be able to request another trial license you must connect to the licensing service database (look in Tools\LicensingService\App_Data folder) and delete the records from the Activations table.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    End Sub

    Private settings As AppSettings

    Private progressForm As ActivationForm

    Private Function CreateLicenseKeyTemplate() As KeyTemplate
        Return New KeyTemplate(5, 5, "doxzyMNu7n46whM=", Nothing, 109, 16, "ProductId")
    End Function

    Private trialLicenseKey As String = "X62DG-94SDT-A4TBZ-949CK-KMZB5"
End Class

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.


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


About the Author

Ansel Reynard
United States United States
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 16 Oct 2012
Article Copyright 2012 by Ansel Reynard
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid