
Selling a Windows application means you need a reliable way to control access and prevent unauthorized use. In this guide, you'll integrate Keyforge into a .NET application using C# to manage license keys.
You'll also see how to connect a payment provider and how to support offline license verifications for users without a constant internet connection.
Before you start
Make sure you've created the basics inside your Keyforge account:
- A product in the dashboard
- One or more licenses in the licenses page
Activating a license
When a user first opens your app, prompt them to enter their license key. Then, send it to the Keyforge API to activate the license on their device.
using System.Net.Http;
using System.Text;
using System.Text.Json;
var client = new HttpClient();
var body = JsonSerializer.Serialize(new {
licenseKey = "ABCDE-ABCDE-ABCDE-ABCDE-ABCDE",
deviceIdentifier = GetDeviceId(),
deviceName = Environment.MachineName,
productId = "p_123456"
});
var response = await client.PostAsync(
"https://keyforge.dev/api/v1/public/licenses/activate",
new StringContent(body, Encoding.UTF8, "application/json")
);
Console.WriteLine("License activated");Getting a device identifier
A reliable way to get a stable device identifier on Windows is to read the machine GUID from the registry:
using Microsoft.Win32;
var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Cryptography");
var machineGuid = key?.GetValue("MachineGuid")?.ToString();You can also generate a UUID on first launch and persist it to a file or the registry.
After activation, store the license key locally (for example, in the app's settings file or the registry) so it can be reused on future launches.
Validating a license
Your app should validate the license key on each launch to ensure it's still active. Use the Keyforge API to check the current status.
var body = JsonSerializer.Serialize(new {
licenseKey = "ABCDE-ABCDE-ABCDE-ABCDE-ABCDE",
deviceIdentifier = GetDeviceId(),
productId = "p_123456"
});
var response = await client.PostAsync(
"https://keyforge.dev/api/v1/public/licenses/validate",
new StringContent(body, Encoding.UTF8, "application/json")
);
using var stream = await response.Content.ReadAsStreamAsync();
using var doc = await JsonDocument.ParseAsync(stream);
var isValid = doc.RootElement.GetProperty("isValid").GetBoolean();
if (isValid) {
Console.WriteLine("License is valid");
} else {
Console.WriteLine("License invalid");
}If the license is invalid, prompt the user to re-enter their key. This check can also be run periodically during the app's lifetime.
Payments and customer portal
Connect a payment provider to automatically generate and deliver a license key by email when someone purchases your app, with no backend or webhooks required. Keyforge supports Stripe, Lemon Squeezy, and Polar for both one-time purchases and subscriptions.
For subscriptions, license expiration dates update automatically on each renewal, and the license is deactivated if a subscription is cancelled. Follow the payments setup guide to connect your product. Customers can manage their licenses, devices, and billing through the customer portal.
License upgrades and renewals
You can offer customers the option to upgrade their license to a higher tier or extend the expiration date of a timed license, directly from the customer portal with no code required. Configure up to 3 upgrade or renewal options per product in the dashboard.
Perpetual fallback
Perpetual fallback lets customers keep using your app with limited functionality after their license expires, rather than being locked out entirely. When enabled on a product, the validate endpoint returns status: "fallbacked" for expired licenses. Note that isValid remains true in this case, so check the status field to differentiate.
using var stream = await response.Content.ReadAsStreamAsync();
using var doc = await JsonDocument.ParseAsync(stream);
var status = doc.RootElement.GetProperty("status").GetString();
if (status == "active") {
// Full access
} else if (status == "fallbacked") {
// License expired - grant limited access based on your product's logic
Console.WriteLine("Running in limited mode");
} else {
// Invalid - prompt the user to re-enter their license key
}Offline licensing
If your app needs to run without an internet connection, Keyforge supports offline license validations using signed JWT tokens. These tokens are issued by Keyforge and can be verified locally using the product's public key.
When license tokens are enabled for a product, the activation response includes a token field. Store this token alongside the license key.
using var stream = await response.Content.ReadAsStreamAsync();
using var doc = await JsonDocument.ParseAsync(stream);
if (doc.RootElement.TryGetProperty("token", out var tokenElement)) {
var token = tokenElement.GetString();
// Persist the token for offline use (file, registry, etc.)
File.WriteAllText("license_token.txt", token);
}On each app launch, load the stored token and verify it locally. See the offline licensing guide for the full implementation, including how to verify the JWT signature using the product's public key.
Conclusion
You now have a complete licensing setup for your Windows application. Keyforge handles license management and payment automation so you can focus on building your product.
For more details, visit the Keyforge documentation.
Simplify your licensing process
Focus on building your product and let us handle licensing. Manage license keys via payments and offer your customers a smooth self-serve experience.