I have done something very similar to this in a previous project.
It can be done easily using MySql, which is usually also installed on most web servers.
First you will need to setup a MySql database on your web server. Call it "myapp_keycodes".
Create 1 table (myapp_KeyCodes), and I suggest it has at least 4 fields: KeyCode, Usage, User and Date.
KeyCode - This field should contain your Key Code and be set to Primary & Unique.
Usage - This can be Bool (True/False) or an integer (0/1)
User - The email address of the user installing your application.
Date - The date the application was installed.
Then, In your WPF application, add a text file named "dbConfig.txt".
Inside the text file, you should include the Server, Database name, User and Password used to connect to the database. Put each item on a new line. see below for example:
99.214.49.191 or http:
myapp_KeyCodes
myapp_user1
Password12345
This could be hard coded in, but would make it difficult should you change web servers.
Next, add the following file to your project. Name it DBConnect.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using MySql.Data.MySqlClient;
namespace myApp
{
partial class DBConnect
{
private MySqlConnection connection;
private string server;
private string database;
private string uid;
private string password;
#region Load Config file information
public DBConnect()
{
var filestream = new FileStream(@"c:\Configfolder\dbConfig.txt",
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite);
var file = new StreamReader(filestream, Encoding.UTF8, true, 128);
string lineOfText;
int x = 1;
while((lineOfText = file.ReadLine()) != null)
{
if (x == 1) { server = lineOfText; }
if (x == 2) { database = lineOfText; }
if (x == 3) { uid = lineOfText; }
if (x == 4) { password = lineOfText; }
x++;
}
Initialize();
}
#endregion
#region Initialize
private void Initialize()
{
string connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}
#endregion
#region Encypt/Decrypt
public string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
public string Base64Decode(string base64EncodedData)
{
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}
#endregion
#region Open Database Connection
public bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0:
MessageBox.Show("Cannot connect to server. Contact administrator");
break;
case 1045:
MessageBox.Show("Invalid username/password, please try again");
break;
case 1042:
MessageBox.Show("Unable to connect to any of the specified MySQL hosts., please try again");
break;
}
return false;
}
}
#endregion
#region Close Database Connection
public bool CloseConnection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
#endregion
#region Get Key Code information
public boolean getKeyCode(string kcode)
{
string query = "SELECT * FROM myapp_KeyCodes where KeyCode = '" + kcode + "'";
if (this.OpenConnection() == true)
{
try
{
MySqlCommand cmd = new MySqlCommand(query, connection);
MySqlDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
string kc = dataReader["KeyCode"].ToSting();
int usage = dataReader["Usage"];
string uName = dataReader["User"].ToString();
string date = dataReader["Date"].ToString();
}
dataReader.Close();
if(kcode == kc)
{
if(usage == 0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
catch
{
return false;
}
}
else
{
return false;
}
}
#region Update Keycode Information.
public void UpdateKeyCode(string kCode, string nUser, string IDate, Int nUsage)
{
string query = "UPDATE myapp_KeyCodes SET User='" + nUser + "', Date='" + IDate + "', Usage='" + nUsage + "' WHERE KeyCode =" + kCode;
if (this.OpenConnection() == true)
{
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = query;
cmd.Connection = connection;
cmd.ExecuteNonQuery();
this.CloseConnection();
}
}
#endregion
}
}
OK, Now comes the part to use this in your application.
For example, this could be inside your submit button or install button that is on the window where it is asking the user to enter the Key Code.
private void Button_Click(object sender, RoutedEventArgs e)
{
DBConnect myDBConnect = new DBConnect();
bool checkCode = getKeyCode(txt_KeyCode.text);
if(checkCode == True)
{
return;
}
else
{
UpdateKeyCode(txt_KeyCode.text, txt.UserEmail.Text, DateTime.Now.ToString(), 1)
}
I tried to put this together for you fairly quickly.
There may be some type-o's or other errors, but should give you the idea of how it works. I've actually used the DBConnect class in several of my applications, and it seem to work very well.
I will be willing to help you further if you can not get this to work.
Good Luck! :)