Modern SaaS and desktop software often requires secure license key generation. While many teams rely on third-party licensing providers, building your own system in ASP.NET Core gives you full control over:
Key format
Activation limits
Expiration & renewal
Machine binding
Offline validation
Logging & tracking
This article shows how to create a professional-grade license key generator and validator using ASP.NET Core 8, including database schema, services, and a secure signing mechanism.
1. Requirements and Design Goals
Your custom licensing system should support:
Key generation
Unique, unpredictable license keys
Expiration date
Optional device/machine binding
Usage count or activation limit
Optional edition-level restrictions (Basic, Pro, Enterprise)
Key validation API
Checks signature
Verifies expiration
Verifies activation count
Registers activations
Security
Use asymmetric cryptography (RSA 2048+)
License keys must be tamper-proof
Server holds private key; client uses public key
Ease of export
Create JSON + printable license files
Support offline verification
2. Database Structure (SQL Server)
License Table
CREATE TABLE Licenses (
LicenseId UNIQUEIDENTIFIER PRIMARY KEY,
CustomerName NVARCHAR(200),
Email NVARCHAR(200),
LicenseKey NVARCHAR(500),
Edition NVARCHAR(50),
ExpirationDate DATETIME,
MaxActivations INT,
CreatedOn DATETIME DEFAULT GETDATE()
);
Activation Table
CREATE TABLE LicenseActivations (
ActivationId UNIQUEIDENTIFIER PRIMARY KEY,
LicenseId UNIQUEIDENTIFIER,
MachineId NVARCHAR(200),
ActivatedOn DATETIME DEFAULT GETDATE()
);
3. License Payload Structure
Before generating a license key, create a payload object that describes the license.
public class LicensePayload
{
public Guid LicenseId { get; set; }
public string Edition { get; set; }
public DateTime ExpirationDate { get; set; }
public int MaxActivations { get; set; }
public string CustomerName { get; set; }
public string Email { get; set; }
}
This payload will be serialized to JSON → signed with RSA → encoded as Base64 → distributed as Key.
4. RSA Key Pair (One-time setup)
Generate RSA keys once:
openssl genpkey -algorithm RSA -out private.pem -pkeyopt rsa_keygen_bits:2048
openssl rsa -pubout -in private.pem -out public.pem
Store private.pem securely (do NOT commit to Git).
Clients will use public.pem to validate offline.
5. License Signing Service (ASP.NET Core)
Load RSA keys
public class RsaKeyService
{
private readonly RSA _privateKey;
private readonly RSA _publicKey;
public RsaKeyService(IWebHostEnvironment env)
{
_privateKey = RSA.Create();
_privateKey.ImportFromPem(File.ReadAllText(Path.Combine(env.ContentRootPath, "Keys/private.pem")));
_publicKey = RSA.Create();
_publicKey.ImportFromPem(File.ReadAllText(Path.Combine(env.ContentRootPath, "Keys/public.pem")));
}
public RSA PrivateKey => _privateKey;
public RSA PublicKey => _publicKey;
}
6. Generating a License Key
public class LicenseGenerator
{
private readonly RsaKeyService _rsa;
public LicenseGenerator(RsaKeyService rsa)
{
_rsa = rsa;
}
public string GenerateLicense(LicensePayload payload)
{
string json = JsonSerializer.Serialize(payload);
// sign the payload
byte[] data = Encoding.UTF8.GetBytes(json);
byte[] signature = _rsa.PrivateKey.SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
// combine payload + signature
var licenseFile = new
{
Payload = payload,
Signature = Convert.ToBase64String(signature)
};
string finalJson = JsonSerializer.Serialize(licenseFile);
return Convert.ToBase64String(Encoding.UTF8.GetBytes(finalJson));
}
}

Join the conversation! Your thoughts help the community grow.