Generate the license key from the unique machine key in C# win forms Setup

Ashok picture Ashok · Dec 27, 2013 · Viewed 16.8k times · Source

I have developed an C# win forms application in Visual Studio 2010 and to provide security to it I am generating a machine dependent key by using systems cpuId, biosId, diskId. It looks like

key

Now in Setup I am just getting one key input area like below.

enter image description here

and I want to show the machine key which is created for the specific system, above the serial key input area.

My need is that the end user or buyer of the Software call me and give me the machine key and then I will calculate a key using that key and send back to client or buyer.

This is my first setup project so I am totally unaware of this thing. I will really appreciate your humble response.

Answer

Kurubaran picture Kurubaran · Dec 27, 2013

I like to break your question into two parts

Creating a UI with required fields or controls where user can provide the license key

There are two way to get the user input during the installation,

  • Creating a windows form with required controls to get the input(You can not open windows form as a modal pop up during the installation)

  • Creating a .wid file to get the user input(This would be the recommended approach)

Validating the license Key and aborting the installation when invalid key is used

Once you have got the user input during the installation you have to validate it, You can use Installer Class for this.

Install() method example

public override void Install(System.Collections.IDictionary stateSaver)
{
    //Invoke the base class method
    base.Install(stateSaver);

    if (!keyEnteredByUser.Equals(generatedKey))
    {
       //This would abort the installation
       throw new Exception("Invalid Key");
    }
}