Learn how to integrate KeyLock license verification into your applications.
Copy this code into your C# console application to verify license keys:
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Management;
using System.Security.Cryptography;
using System.Threading.Tasks;
namespace KeyLock
{
// Ready-to-use KeyLock client library
// Just set your product ID and API key, and you're good to go!
public class KeyLockClient
{
private readonly string _productId;
private readonly string _apiKey;
private readonly string _serverUrl;
private readonly HttpClient _httpClient;
public KeyLockClient(string productId, string apiKey, string serverUrl = "https://v0-keyauth-like-project.vercel.app")
{
_productId = productId;
_apiKey = apiKey;
_serverUrl = serverUrl;
_httpClient = new HttpClient();
}
// Verify a license key
public async Task<LicenseResult> VerifyLicenseAsync(string licenseKey)
{
try
{
string hwid = GetHardwareId();
var requestData = new
{
licenseKey = licenseKey,
productId = _productId,
apiKey = _apiKey,
hwid = hwid
};
var json = JsonSerializer.Serialize(requestData);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync($"{_serverUrl}/api/license/verify", content);
var responseJson = await response.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var result = JsonSerializer.Deserialize<ApiResponse>(responseJson, options);
return new LicenseResult
{
IsValid = result.Valid,
Message = result.Message,
ExpiresAt = result.ExpiresAt,
HardwareId = hwid
};
}
catch (Exception ex)
{
return new LicenseResult
{
IsValid = false,
Message = $"Error: {ex.Message}"
};
}
}
// Get hardware ID
public static string GetHardwareId()
{
try
{
// Get processor ID
string cpuId = "";
using (var searcher = new ManagementObjectSearcher("SELECT ProcessorId FROM Win32_Processor"))
{
foreach (var obj in searcher.Get())
{
cpuId = obj["ProcessorId"].ToString();
break;
}
}
// Get motherboard serial number
string motherboardSerial = "";
using (var searcher = new ManagementObjectSearcher("SELECT SerialNumber FROM Win32_BaseBoard"))
{
foreach (var obj in searcher.Get())
{
motherboardSerial = obj["SerialNumber"].ToString();
break;
}
}
// Combine hardware identifiers and hash them
string combinedId = $"{cpuId}|{motherboardSerial}|{Environment.MachineName}";
using (var sha256 = SHA256.Create())
{
var hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(combinedId));
return BitConverter.ToString(hashBytes).Replace("-", "");
}
}
catch
{
// Fallback to machine name if hardware info can't be retrieved
return Environment.MachineName;
}
}
// Response classes
private class ApiResponse
{
public bool Success { get; set; }
public bool Valid { get; set; }
public string Message { get; set; }
public string ExpiresAt { get; set; }
}
}
public class LicenseResult
{
public bool IsValid { get; set; }
public string Message { get; set; }
public string ExpiresAt { get; set; }
public string HardwareId { get; set; }
public int DaysRemaining
{
get
{
if (string.IsNullOrEmpty(ExpiresAt)) return 0;
try
{
var expiryDate = DateTime.Parse(ExpiresAt);
return (int)(expiryDate - DateTime.Now).TotalDays;
}
catch
{
return 0;
}
}
}
}
}
// Example usage of the KeyLock client library
using System;
using System.Threading.Tasks;
using KeyLock;
class Program
{
// Your KeyLock credentials - only these need to be changed!
private const string PRODUCT_ID = "YOUR_PRODUCT_ID";
private const string API_KEY = "YOUR_API_KEY";
static async Task Main(string[] args)
{
Console.WriteLine("=== KeyLock License Verification ===");
Console.WriteLine();
// Ask for license key
Console.Write("Enter your license key: ");
string licenseKey = Console.ReadLine();
if (string.IsNullOrWhiteSpace(licenseKey))
{
Console.WriteLine("License key cannot be empty.");
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
return;
}
try
{
// Create the KeyLock client
var client = new KeyLockClient(PRODUCT_ID, API_KEY);
// Verify the license
Console.WriteLine("Verifying license...");
var result = await client.VerifyLicenseAsync(licenseKey);
if (result.IsValid)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("✓ License is valid!");
if (!string.IsNullOrEmpty(result.ExpiresAt))
{
Console.WriteLine($"License expires on: {result.ExpiresAt}");
Console.WriteLine($"Days remaining: {result.DaysRemaining}");
}
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("✗ License verification failed!");
Console.WriteLine($"Reason: {result.Message}");
Console.ResetColor();
}
Console.WriteLine($"Hardware ID: {result.HardwareId}");
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Error: {ex.Message}");
Console.ResetColor();
}
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
YOUR_PRODUCT_ID
with your product ID from your products pageYOUR_API_KEY
with your API key from your profile pagePOST https://v0-keyauth-like-project.vercel.app/api/license/verify
Request Body:
{
"licenseKey": "YOUR-LICENSE-KEY",
"productId": "YOUR-PRODUCT-ID",
"apiKey": "YOUR-API-KEY",
"hwid": "HARDWARE-ID",
"ip": "CLIENT-IP-ADDRESS" // Optional
}
Response:
{
"success": true,
"valid": true,
"message": "License is valid",
"expiresAt": "2023-12-31T23:59:59Z"
}