Click here to Skip to main content
12,622,568 members (28,509 online)
Click here to Skip to main content

Stats

93.8K views
15.8K downloads
230 bookmarked
Posted

Creating Secure Trial Versions for .NET Applications - A Tutorial

, 16 Oct 2012 CPOL
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()
        settings.Load()

        BeginLicenseValidation()
    End Sub

    Private Sub BeginLicenseValidation()
        Dim _licenseValidationThread As Thread = New Thread(New ParameterizedThreadStart(AddressOf LicenseValidationThread))
        _licenseValidationThread.Start()
    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)"
                Else
                    registrationStatus = "REGISTERED"
                End If
            Else
                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
        settings.Save()
    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)
        activationForm.ActivateLicense()

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

            BeginLicenseValidation()
        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", "")

        settings.Save()

        txtLicenseKey.Clear()
        txtActivationKey.Clear()
        txtActivationHardwareId.Clear()
        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
        Close()
    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.

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

You may also be interested in...

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