Product Key Management System

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
72 posts Page 1 of 8
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Product Key Management System
CodenStuff
Hello,


I have got a new application tutorial for you all. The application you create using this tutorial will give you a product activation management system for all your programs. You put the trial code into your existing applications and keep the activation manager on your own computer to manage the people who purchase your product keys. Here is a quick list of what it does:

Image

Inside your application
30-day Trial feature (can be changed to however long you want your application to work before it needs activating)
Purchase Activation key (through paypal, nochex or any payment system you use)
Instant Activation (can be activated before the end of trial)
Once the user activates there key it is disabled so it cannot be used again by someone else


Inside management system
Create username and randomly generated product key
Activation details E-mailed to the user
Check which users have activated your application
Give users 48-hours to use the key and if the key isnt used within this time then you can deactivate it (this prevents people shareing keys)


In order to use this system you will need somewhere to store the product keys and an smtp email server. I recommend using these FREE services http://www.0catch.com/ as a free webhost and http://www.gmx.com is the best free smtp email provider.

Here are a couple more free FTP hosts: http://www.orgfree.com .... http://www.nofeehost.com

OK lets get started. First I will show you how to add the trial and activation features to your existing application(s) so open up one of your apps or start a new project and do the following:

In the navigation menu click "Project > YourApp Properties" and then click the "Settings" tab on the left hand side.

Now we need to enter 3 new settings in here so ill tell you the name of the setting and its value which you need to enter

apptrial - value = 1
trialcode - value = leave this blank
activated - value = 0

OK close that and save the settings.



You need to add a button control somewhere on your form to be used as an activation button. Once you have done that you need to go into the code of your projects main form and enter the followin code inside the 'Form_Load' event:
Code: Select all
Dim tcode = Convert.ToString(My.Settings.activated)
        If (tcode = "1") Then
            Button1.Visible = False
        Else
            isittrial()
        End If
You need to add this code within your 'Activation' buttons click event:
Code: Select all
Dim theSecondForm As New Activate()
        theSecondForm.ShowDialog()

Then add this just above the code line that read "End Class"
Code: Select all
Private Sub isittrial()
        Dim trialtime As Date = Now
        Dim currentdatetime As Date = Now
        trialtime = trialtime.AddDays(30) 'SET THIS TO HOWEVER MANY DAYS YOU WANT THE TRIAL TO RUN FOR
        Dim ttime = Convert.ToString(My.Settings.apptrial)
        If (ttime = "1") Then
            My.Settings.trialcode = trialtime
            My.Settings.apptrial = "0"
            My.Settings.Save()
        End If
        If (currentdatetime > trialtime) Then
            Dim theSecondForm As New Activate()
            theSecondForm.ShowDialog()
            Me.Close()
        End If
    End Sub

Now press "Ctrl+Shft+A" and add a new "FORM" to your project and call it "Activate.vb". In the new form you need to add the following controls and arrange them as you like:

Button1 = Used to verify the activation key
Button2 = Used to purchase a key
TextBox1 = Username will go in here
TextBox2 = Activation key will go in here

Then once you are happy with the layout of your controls please go into "Activate.vb" forms code-view and replace the code inside with this entire block:
Code: Select all
Imports System.Net
Imports System.IO
Public Class Activate
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'GET SERIALS FROM SITE
        Try
            Dim url As String = "http://www.YOURSITE.com/" + TextBox1.Text + ".act"
            Dim pageRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
            Dim pageResponse As WebResponse = pageRequest.GetResponse()
            Dim page As String = ""
            Using r As New StreamReader(pageResponse.GetResponseStream())
                page = r.ReadToEnd()
            End Using

            'END SERIALS
            If TextBox2.Text = page Then
                My.Computer.FileSystem.WriteAllText(Application.StartupPath + "\" + TextBox1.Text + ".act", "****KEY-USED****", True)
                My.Settings.trialcode = page
                My.Settings.activated = "1"
                My.Settings.Save()
                My.Computer.Network.UploadFile(Application.StartupPath + "\" + TextBox1.Text + ".act", "ftp://www.YOURSITE.com/" + TextBox1.Text + ".act", "USERNAME", "PASSWORD", True, 10, FileIO.UICancelOption.DoNothing)
                If My.Computer.FileSystem.FileExists(Application.StartupPath + "\" + TextBox1.Text + ".act") Then
                    My.Computer.FileSystem.DeleteFile(Application.StartupPath + "\" + TextBox1.Text + ".act", FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)
                End If
                MsgBox("ACTIVATION COMPLETE.  THANK YOU FOR YOUR SUPPORT.")
                Application.Restart()
            Else
                MsgBox("Invalid Key!")
                Me.Close()
            End If
        Catch ex As Exception
            MsgBox("Username not valid.  Please enter the correct username.")
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        System.Diagnostics.Process.Start("iexplore.exe", "http://www.paypal.com")
    End Sub
End Class
Now there are 5 things you need to change in the code you just copied and they are very important so I will explain as best I can how you change them. I will list the code and what you need to change it to:

http://www.YOURSITE.com/
Change this to your website address, you MUST end the address with a slash "/" or you will get errors when running the program.

ftp://www.YOURSITE.com/
Change this to your websites FTP address. Again the slash "/" at the end is important.

USERNAME
Change this to your FTP username.

PASSWORD
Change this to your FTP password

http://www.paypal.com
If you want to charge people for the activation keys then for whichever payment website you choose to use you will need to change this address to your payment URL. For example if you use paypal you would need to first setup a product to sell and then paypal will give you a payment URL for that product you create, you would then paste that URL here. Payments are manual so when someone pays you and you have verified the payment you would then use the activation management system below to send them an email with there username and key.


Once you have changed all the settings to your satisfaction you can now save and build your application because it is now 'Trial Activated'. From now on whenever someone uses your application they will only be able to use it for 30-days (or whatever you chose for this option).

And thats the end of part one.


Now we will move on to the next step which is to create the product activation management system. So lets start a new project and call it something like "ProductManager".

First add the following controls to your form and they will be used as explained:

Textbox1 = Email address to send the key to
Textbox2 = Enter any username here
Textbox3 = The product key will be generated in here
Textbox4 = Used to check who has used there key and whos key has expired

Combobox1 = Used to select which of your programs the key your sending is for
Combobox2 = Used to select users and check activation status

Button1 = Generates a random product key
Button2 = Sends the key via email and uploads to website
Button3 = Used to deactivate a users key if its out-of-date

Label1 = Used to indicate the status of an account if its active or inactive

Arrange those controls on your form however you like so you have something that will look a little like this:

Image

OK now set button2 and button3's "Enabled" propertie to 'False'. And now its time for the code and you can just copy this entire block into your forms code screen:
Code: Select all
Imports System.Net
Imports System.IO
Imports System.Net.mail
Imports System.Data
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim rand As New Random()
        Dim length As Integer
        Dim randomString As String
        length = rand.Next(15, 15)
        randomString = ""
        For i As Integer = 1 To length
            randomString &= Chr(65 + rand.Next(20))
        Next
        Dim x As Integer
        Dim randnum As New Random
        x = rand.Next(10000000, 500000000)
        TextBox3.Text = randomString
        TextBox3.Text = TextBox3.Text + x.ToString
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If TextBox1.Text = "" Then
            MsgBox("Email address is blank")
        ElseIf TextBox2.Text = "" Then
            MsgBox("Username is blank")
        End If
        'Sends the email and attachment
        Dim trialtime As Date = Now.AddDays(1)
        Dim from As New MailAddress("your@email.com")
        Dim [to] As New MailAddress(TextBox1.Text)
        Dim theMailMessage As New MailMessage(from, [to])
        theMailMessage.Body = "Thank you for purchasing " + ComboBox1.Text + vbNewLine + vbNewLine + "Your activation details are below." + vbNewLine + vbNewLine + "Username: " + TextBox2.Text + vbNewLine + vbNewLine + "Product Key: " + TextBox3.Text
        theMailMessage.Subject = ComboBox1.Text + " Product Key"
        Dim theClient As New SmtpClient("email.server.com")
        theClient.UseDefaultCredentials = False
        Dim theCredential As New System.Net.NetworkCredential("eusername", "epassword")
        theClient.Credentials = theCredential
        theClient.EnableSsl = True
        Try
            theClient.Send(theMailMessage)
        Catch wex As Net.WebException
            MessageBox.Show("Could not send email...check your settings.")
        End Try
        MsgBox("E-mail successfully sent to:" + TextBox1.Text)
        ComboBox2.Items.Add(TextBox2.Text)
        My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\" + TextBox2.Text + ".usr", trialtime, False)
        My.Computer.FileSystem.WriteAllText(Application.StartupPath + "\" + TextBox2.Text + ".act", TextBox3.Text, True)
        My.Computer.Network.UploadFile(Application.StartupPath + "\" + TextBox2.Text + ".act", "ftp://www.YOURSITE.com/" + TextBox2.Text + ".act", "USERNAME", "PASSWORD", True, 10, FileIO.UICancelOption.DoNothing)
        If My.Computer.FileSystem.FileExists(Application.StartupPath + "\" + TextBox2.Text + ".act") Then
            My.Computer.FileSystem.DeleteFile(Application.StartupPath + "\" + TextBox2.Text + ".act", FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)
        End If
        'ComboBox1.Items.Add(TextBox1.Text)
        If My.Computer.FileSystem.FileExists(Application.StartupPath & "\Accounts.act") Then
            My.Computer.FileSystem.DeleteFile(Application.StartupPath & "\Accounts.act", FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)
        End If
        With ComboBox2
            Dim i As Integer
            Dim s As String = ""
            For i = 0 To .Items.Count - 1
                s += .Items(i).ToString & vbNewLine
            Next

            My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\Accounts.act", s, False)
        End With
        MsgBox("Activation Key Successfully Uploaded")
    End Sub

    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
        If TextBox3.Text = "" Then
            Button2.Enabled = False
        Else
            Button2.Enabled = True
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Retrieve file list from PlayList.txt file to ListBox1
        If My.Computer.FileSystem.FileExists(Application.StartupPath & "\Accounts.act") Then
            Using sr As New IO.StreamReader(Application.StartupPath & "\Accounts.act")
                Dim item As String = sr.ReadLine
                While item <> Nothing
                    ComboBox2.Items.Add(item)
                    item = sr.ReadLine
                End While
            End Using
        End If
    End Sub

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        If My.Computer.FileSystem.FileExists(Application.StartupPath & "\" + ComboBox2.Text + ".usr") Then
            Using sr As New IO.StreamReader(Application.StartupPath & "\" + ComboBox2.Text + ".usr")
                TextBox4.Text = sr.ReadLine
            End Using
        End If
        Dim currentdatetime As Date = Now.AddDays(1)
        Try

            Dim url As String = "http://www.YOURSITE.com/" + ComboBox2.Text + ".act"
            Dim pageRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
            Dim pageResponse As WebResponse = pageRequest.GetResponse()
            Dim page As String = ""
            Using r As New StreamReader(pageResponse.GetResponseStream())
                page = r.ReadToEnd()
            End Using
            If page = "****KEY-USED********KEY-USED****" Then
                Label1.ForeColor = Color.Green
                Label1.Text = "Activated"
            Else
                Label1.ForeColor = Color.Red
                Label1.Text = "Not Activated"
                If (currentdatetime > TextBox4.Text) Then
                    Button3.Enabled = True
                End If
            End If

        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        My.Computer.FileSystem.WriteAllText(Application.StartupPath + "\" + ComboBox2.Text + ".act", "****KEY-USED****", True)
        My.Computer.Network.UploadFile(Application.StartupPath + "\" + ComboBox2.Text + ".act", "ftp://www.YOURSITE.com/" + ComboBox2.Text + ".act", "USERNAME", "PASSWORD", True, 10, FileIO.UICancelOption.DoNothing)
        If My.Computer.FileSystem.FileExists(Application.StartupPath + "\" + ComboBox2.Text + ".act") Then
            My.Computer.FileSystem.DeleteFile(Application.StartupPath + "\" + ComboBox2.Text + ".act", FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)
        End If
        If My.Computer.FileSystem.FileExists(Application.StartupPath + "\" + ComboBox2.Text + ".usr") Then
            My.Computer.FileSystem.DeleteFile(Application.StartupPath + "\" + ComboBox2.Text + ".usr", FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)
        End If
        ComboBox2.Items.Remove(ComboBox2.Text)
        If My.Computer.FileSystem.FileExists(Application.StartupPath & "\Accounts.act") Then
            My.Computer.FileSystem.DeleteFile(Application.StartupPath & "\Accounts.act", FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)
        End If
        With ComboBox2
            Dim i As Integer
            Dim s As String = ""
            For i = 0 To .Items.Count - 1
                s += .Items(i).ToString & vbNewLine
            Next

            My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\Accounts.act", s, False)
        End With
        Button3.Enabled = False
        MsgBox("Account has been deactivated!")
    End Sub
End Class
Dont forget to change the settings in this code aswell like you did in the last block of code, like this:

http://www.YOURSITE.com/
Change this to your website address, you MUST end the address with a slash "/" or you will get errors when running the program.

ftp://www.YOURSITE.com/
Change this to your websites FTP address. Again the slash "/" at the end is important.

USERNAME
Change this to your FTP username.

PASSWORD
Change this to your FTP password

your@email.com
Change this to your SMTP email address

mail.server.com
Change this to your SMTP email server address

eusername
Change to your SMTP email username

epassword
Change to your SMTP email password

And thats all you need to do :). save and build your project and you now have a fully working activation management system that works!.

***************************************************************************************************************************
There is an update to this tutorial at this URL: Product Key Manager Update
***************************************************************************************************************************

Happy coding.
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Product Key Management - Download
CodenStuff
Hello,

As a new version of this application is now available in the VIP section I have now made the first release available for general download. This version is the source-code for the tutorial above.

Source-Code:
SerialManagerv1.zip
Enjoy cooll;
You do not have the required permissions to view the files attached to this post.
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
hungryhounduk
VIP - Site Partner
VIP - Site Partner
Posts: 2870
Joined: Mon Jul 27, 2009 11:58 am

Hi
Once again a Quality Production from CodenStuff cooll;

respect
Image
User avatar
CodemaN
VIP Access - Denied
VIP Access - Denied
Posts: 74
Joined: Fri Sep 18, 2009 3:18 pm

Re: Product Key Management System
CodemaN
thank you man!!!
User avatar
c0d3r n3rd
Just Registered
Just Registered
Posts: 5
Joined: Sat Oct 24, 2009 12:07 pm

Re: Product Key Management System
c0d3r n3rd
wow man a lil bit of helpplz cause it wont work for me when click generate key it generates the key and when i click send key it sends the eky to the mail address but when it says uploadng file it gives and eror an it just goes not responding so could you plz help man? im using zymic for the webhost.
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Hello,

Zymic has been having serious problems recently and hasnt been working very well. I recommend using http://www.0catch.com/ for now.

Happy coding! cooll;
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
Nery
Co-Admin Moderator
Co-Admin Moderator
Posts: 1117
Joined: Mon Sep 07, 2009 8:11 pm

Re: Product Key Management System
Nery
Yes, I've tried zymic and it doesn't work well, it always blows in the upload progress of any file.
User avatar
gilles
Member
Member
Posts: 43
Joined: Sun Oct 25, 2009 2:37 pm

Re: Product Key Management System
gilles
amazing!
i love it!
keep going man!
Dont forget!
people are watching first the design then the program ;)
User avatar
cricketgd
Just Registered
Just Registered
Posts: 3
Joined: Sun Nov 01, 2009 7:58 pm

Re: Product Key Management System
cricketgd
IF YOU DO ANYTHING --PLEASE HELP ME!-- . I have downloaded it and edited it and done everything! Edited the code etc... When I click send email, it comes up with an error and when in debugging mode it highlights in yellow the code:theClient.Send(theMailMessage) . Please help me as I really need this soon for my realase of a new program! Thanks in advance, Cricketgd!
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Hello,

Welcome to codenstuff.com cricketgd.

Are you certain you have entered all the settings correctly?. What SMTP email host are you using?

Thank you.
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
72 posts Page 1 of 8
Return to “Tutorials”