Click here to Skip to main content
Licence CPOL
First Posted 20 Sep 2007
Views 17,498
Downloads 250
Bookmarked 48 times

Contacting a Registration Server to obtain a unique registration key based on unique machine ID

By | 20 Sep 2007 | Article
This article describes usage of System.Net.WebRequest & WebResponse to contact a Server and obtain a registration key for a Windows application.
Screenshot - registration.jpg

Introduction

Sometimes in the industry, it is required to collect some unique machine information and send it to a server for the purpose of creating a unique registration key based on the hardware information. This is required in the case when the software requires a per PC registration. Thus we can collect the machine information of the PC in which the software was installed, and issue a registration key which will be valid only for that machine.

Background

Using WMI functionality, it is possible to collect machine specific hardware information.
In .NET, one can use System.Management.ManagementClass for this purpose. (This is explained in this article.)

The problem arises when we try to send this information to the server. Because of the firewall that is present in OSes starting from WinXP, if we try to send data from our application to an external server, the firewall pops up a warning to the user informing her/him that the application is trying to send data and asks the user whether she/he wants to proceed or block the application. (Also this approach will fail if the local network is connected to the Internet through a firewall, which is configured to block anything other than HTTP traffic.)

The solution thus comes in the form of HTTP (i.e. we have to send the data through HTTP).
In .NET, this is easily achieved through System.Net.WebRequest or by using a Web Service. This article uses the WebRequest approach.

Using the Code

In the class ContactRegistationServer (UserRegistration project), you will notice that we are creating a WebRequest and passing the hardware information through QueryString.

WebResponse registrationKey = null;
String url_string = "http://localhost/Registration/Registration.aspx"
    + "?CPU=" + System.Web.HttpUtility.UrlEncode(hardwareInfo.cpuID)
    + "&BIOS=" + System.Web.HttpUtility.UrlEncode(hardwareInfo.biosID)
    + "&MB=" + System.Web.HttpUtility.UrlEncode(hardwareInfo.baseID)
    + "&DISK=" + System.Web.HttpUtility.UrlEncode(hardwareInfo.diskID)
    + "&MAC=" + System.Web.HttpUtility.UrlEncode(hardwareInfo.macID)
    + "&VIDEO=" + System.Web.HttpUtility.UrlEncode(hardwareInfo.videoID);
try
{
    WebRequest requestRegistration = WebRequest.Create(url_string);
    registrationKey = requestRegistration.GetResponse();
}
catch (Exception ex)
{
    System.Windows.Forms.MessageBox.Show("Failed connecting to server");
    return;
}

On the server (Refer project 'TestPage' Registration.aspx.cs), the QueryString received from the client is retrieved and a unique registration key is produced and returned to the user.

//Call the Foolproof Registration Key algorithm :)
TheFoolProofRegistrationKey regKey = new TheFoolProofRegistrationKey();
String key = regKey.getRegistrationKey(Request.QueryString["CPU"]);
Response.Write(key);

Notes: If You Decide to Download and Run the Program

If you decide to download and try the application, please make sure to create a virtual folder of the name "Registration" on your local Web server. This is required as the WebRequest is using the following URL: http://localhost/Registration/Registration.aspx (or else copy the files to a folder by the name Registration under the server root folder).

Just for the use of a novice, I have attached a few screen shots on how to add the virtual directory on IIS.

Screenshot - virtual3.jpg

And finally you should have something as follows:

Screenshot - virtual2.jpg

Points of Interest

For testing purposes, I also tried to connect to the server from a different PC: (e.g.: http://10.10.42.135/Registration/Registration.aspx) and it still worked - the firewall did not complain:).
I did not try to connect to a server outside our network, but I guess that should also work...

To collect machine specific information, use the code in this article.

History

  • 20th September, 2007: Initial post

License

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

About the Author

Sajjitha Gunawardana

Web Developer

Sri Lanka Sri Lanka

Member

coding in C# C++ C
 
==============================================
 
Code in English, not Gibberish Smile | :)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionD we need any Crypto library? PinmemberGiri Ganji19:33 24 Sep '07  
AnswerRe: D we need any Crypto library? PinmemberSajjitha Gunawardana5:07 25 Sep '07  
GeneralOne Caveat PinmemberTrendyTim12:56 24 Sep '07  
GeneralGood Article Pinmembermerlin9814:17 21 Sep '07  
Good Article, but I have a few suggestions:
 
1) Instead of using "+" to concatenate strings, use string.format() instead. It is much faster and more efficient. There are several articles on Code Project that explain why string.format is better than "+".
 
2) I would suggest that you encrypt the data in some form before sending it, especially if your software is to be used by companies that have HIPPA or other regulatory compliance laws.
 
3) You can also setup a webservice to perform the key creation/validation. This might be easier to setup and use for most developers.
 
4) Some firewalls, such as Zone Alarm, can prevent individual applications from accessing the internet. I would suggest adding something to the client code that prevents further use of the client until a key can be generated.
 


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I get my developer tools from Merlin A.I. Soft

Rhabot - World of Warcraft Bot

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

GeneralRe: Good Article PinmemberSajjitha Gunawardana5:26 21 Sep '07  
GeneralRe: Good Article Pinmembermerlin9817:45 21 Sep '07  
GeneralRe: Good Article PinmemberMathiasW12:47 24 Sep '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 20 Sep 2007
Article Copyright 2007 by Sajjitha Gunawardana
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid