It’s important that you don’t make this information easy for someone who’s trying to break your system, so this denomination is not a good one.
Change to the following:
public class Licenca
{
[Key]
public string Chave { get; set; }
}
The right way is to use a Migration to generate key information for you. Something like this:
namespace SeuProjeto.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
public sealed class Configuration : DbMigrationsConfiguration<SeuProjeto.Models.SeuProjetoContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
protected override void Seed(SeuProjeto.Models.SeuProjetoContext context)
{
// Estou supondo que você implementou os métodos dentro de uma classe
// estática, então ficaria:
var chave = LicencasHelper.GerarLicenca();
context.Licencas.AddOrUpdate(
l => l.Chave,
new Licenca
{
Chave = chave
});
context.SaveChanges();
}
}
}
The class LicencasHelper
would be something like:
public static class LicencasHelper
{
public static string GerarLicenca()
{
return GetSHA1HashData(GetMacAddress());
}
private static string GetSHA1HashData(string data)
{
SHA1 sha1 = SHA1.Create();
byte[] hashData = sha1.ComputeHash(Encoding.Default.GetBytes(data));
StringBuilder returnValue = new StringBuilder();
for (int i = 0; i < hashData.Length; i++)
{
returnValue.Append(hashData[i].ToString());
}
return returnValue.ToString();
}
private static string GetMacAddress()
{
string macAddresses = string.Empty;
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.OperationalStatus == OperationalStatus.Up)
{
macAddresses += nic.GetPhysicalAddress().ToString();
break;
}
}
return macAddresses;
}
}
In subsequent publications, consider commenting on the code of Migration in order to prevent reverse engineering.
Another thing that can be done is to separate the code from the Helper in a DLL just for it, and remove the reference when the Migration is performed, but this is only if you do not need to check the license on login.
In any case, a Action
login would look like this:
public ActionResult Login()
{
var licenca = context.Licencas.FirstOrDefault();
if (licenca == null) return View("SemLicenca");
if (licenca.Chave != LicencasHelper.GerarLicenca()) return View("LicencaInvalida");
return View();
}
Will you use a separate table to save the address? At what point of system use should you take the data and insert it?
– Guilherme Portela
Exact. So the application already startar the first time already keeps the hash. And the subsequent times will only make the check.
– Érik Thiago