How to Implement Software Licensing in C# and .NET
For C# and .NET applications, install the NetLicensingClient-csharp NuGet package, create a Context with your API key, and call LicenseeService.validate(...) with the customer’s licensee number. You get one entry back per product module, with valid and, depending on the licensing model, an expiry date, features or remaining credits.
Install
dotnet add package NetLicensingClient-csharp
or with the Package Manager console:
PM> Install-Package NetLicensingClient-csharp
Source code and releases: Labs64/NetLicensingClient-csharp.
Validate a customer
using NetLicensingClient;
using NetLicensingClient.Entities;
public static class LicenseCheck
{
public static bool IsLicensed(string licenseeNumber)
{
Context context = new Context();
context.securityMode = SecurityMode.APIKEY_IDENTIFICATION;
context.apiKey = "YOUR_API_KEY"; // role ROLE_APIKEY_LICENSEE
ValidationParameters parameters = new ValidationParameters();
parameters.setProductNumber("YOUR_PRODUCT_NUMBER"); // needed only for licensee auto-create
ValidationResult result = LicenseeService.validate(context, licenseeNumber, parameters);
Composition module = result.getProductModuleValidation("YOUR_PRODUCT_MODULE_NUMBER");
return module != null && bool.Parse(module.properties["valid"].value);
}
}
The default base URL is https://go.netlicensing.io/core/v2/rest; change context.baseUrl to use the backup host netlicensing.labs64.com or a NetLicensing Agent.
Licensing-model parameters
// Node-Locked: device fingerprint generated by your application
parameters.put("M-NODE", "nodeSecret", deviceFingerprint);
// Floating: check out a concurrent session
parameters.put("M-FLOAT", "sessionId", sessionId);
parameters.put("M-FLOAT", "action", "checkOut");
// Pay-per-Use: credits consumed since the last validation
parameters.put("M-PPU", "usedQuantity", "5");
Each licensing model page lists its parameters and returned values.
Cache the result
result.getTtl() tells you how long the result stays good. Keep it in memory or in protected storage and validate again once that time has passed:
if (cached == null || DateTime.UtcNow > cached.getTtl().ToUniversalTime())
{
cached = LicenseeService.validate(context, licenseeNumber, parameters);
}
Verify signed responses
Give the context the public half of the RSA key pair whose private key is attached to your API key. From then on the client checks the signature of every validation response. Signing the Response walks through the setup.
context.apiKey = apiKeyWithPrivateKey;
context.publicKey = publicKeyPem;
Validate offline with a license file
The C# client can also read a signed offline validation file with no connection at all:
Context context = new Context();
context.publicKey = publicKeyPem;
ValidationResult result = ValidationService.validateOffline(context, offlineFileXml);
If the signature doesn’t check out, you get a NetLicensingException. See Offline License Validation for creating the file and its limits.
Unity and games
The C# client can also be used in Unity projects, see Can I use NetLicensing in Unity3D projects?.