Seeding a Keyright license from a WiX/MSI installer
Enterprise desktop software does not get activated by the person who runs it. It is pushed out silently through Group Policy, Intune or SCCM with msiexec /qn, to machines whose users never see a dialog and often cannot reach your licensing service. This how-to builds the deployment most ISVs end up needing: an MSI that places a signed machine-wide license under ProgramData so the app is already licensed on first launch, plus the alternative of passing an activation key as an MSI property and letting the app claim it once online. It covers the WiX v5 authoring, where the file must live so every Windows user on the box shares one seat, the deferred custom action that writes a key the app will pick up, the SDK options that make ProgramData authoritative, and the msiexec command lines an admin pastes into their deployment tool.
A consumer app asks the person at the keyboard for a license key. Enterprise software almost never gets to. It arrives on a laptop through Intune, through a Group Policy software-installation GPO, through an SCCM task sequence — msiexec /i Acme.msi /qn run by the SYSTEM account, no window, no user, often no route to the public internet at all. If your first-run experience is “type your key here,” that deployment lands on a few thousand machines showing an activation dialog nobody is sitting in front of.
So the license has to be in place before the first launch, put there by the installer. This post builds that for a .NET desktop app licensed with Keyright, using WiX v5 for the MSI. There are two shapes — bundle a signed site license, or pass an activation key as a property — and the first is the one you want for anything air-gapped. We will do both, and spend most of the time on the detail that makes or breaks it: where the file goes.
The wrong place: per-user AppData
The Keyright SDK’s default home for an activated lease and an offline license is {LocalApplicationData}\Keyright\{product}\, i.e. %LocalAppData% — a per-user folder. That is exactly right when a user activates the app themselves. It is exactly wrong for an installer, for two reasons.
An MSI running under a silent machine-wide install executes as NT AUTHORITY\SYSTEM. Its %LocalAppData% is C:\Windows\System32\config\systemprofile\AppData\Local — a folder no interactive user will ever read. And even if you impersonated the installing user, a machine deployment does not know which user will run the app; it may be ten different people on a shared workstation, or a user who has not logged in yet when the install runs.
A machine-licensed deployment wants one license, readable by every user on the box, written once by the installer. That is what %ProgramData% (CommonAppDataFolder, C:\ProgramData) is for: machine-wide, world-readable, and the natural home for a license that belongs to the device rather than to a person.
Shape 1: bundle a signed site license
For a volume or site customer you issue one signed license document and ship it inside the MSI. Because a Keyright license is a signed payload whose security is the signature and not secrecy, it is safe to carry in an installer; an edited byte invalidates it. Issue it perpetual (or node-locked to the deployment if you node-lock site boxes) so nothing on the end machine has to call home.
The WiX authoring is an ordinary component that lays the file into a ProgramData subfolder:
<!-- Acme.Studio.wxs — WiX v5 -->
<Package Name="Acme Studio" Manufacturer="Acme Ltd"
Version="4.2.0" UpgradeCode="7c3f9e10-7b22-4a8f-9f2e-6d4c1b0a55e1"
Scope="perMachine">
<StandardDirectory Id="CommonAppDataFolder">
<Directory Id="AcmeData" Name="Keyright">
<Directory Id="AcmeProduct" Name="acme-studio" />
</Directory>
</StandardDirectory>
<!-- The signed site license, shipped in the MSI, written to ProgramData. -->
<Component Id="SiteLicense" Directory="AcmeProduct" Guid="*">
<File Id="LicenseJson" Source="licenses\site-license.json" Name="license.json" />
</Component>
<Feature Id="Main">
<ComponentRef Id="SiteLicense" />
<!-- app files … -->
</Feature>
</Package>
CommonAppDataFolder resolves to C:\ProgramData, so the file lands at C:\ProgramData\Keyright\acme-studio\license.json. A per-machine install gives that tree an ACL that every user can read and only administrators can write — which is exactly what you want for a license: readable by whoever runs the app, not editable by them.
Now the SDK has to look there. By default it reads the per-user path, so for a machine-licensed product point DefaultLicensePath (the ambient fallback location) at the ProgramData copy:
using Keyright;
static KeyrightClient CreateLicenseClient()
{
string machineWide = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
"Keyright", "acme-studio");
var options = new KeyrightOptions
{
Product = "acme-studio",
PublicKeyBase64 = AcmeKeys.LicensePublicKey, // public key only; safe to ship
ServiceUrl = "https://licensing.acme.example",
DefaultLicensePath = Path.Combine(machineWide, "license.json"),
// Lease/state stay per-user; only the seeded license is machine-wide.
};
return KeyrightClient.Initialize(options);
}
DefaultLicensePath is the ambient source — it is consulted after an explicit LicenseFilePath or the KEYRIGHT_LICENSE environment variable but before the app falls back to “no license.” That precedence matters: a site customer who later buys a per-seat upgrade can drop a newer file at the explicit path or set the env var without you re-cutting the MSI, and it wins over the seeded one. On first launch client.Validate() finds the seeded document, verifies it offline against the embedded public key, and the app is licensed with no dialog and no network.
LicenseInfo info = client.Validate(out LicenseSourceKind source);
// info.IsValid == true, source == LicenseSourceKind.LicenseFile,
// info.Licensee == "Globex Corporation", info.ExpiryUtc == null
Because the seeded license is perpetual, the SDK writes no local state for it — no lease, no first-seen timestamp — so there is nothing per-user to go wrong and nothing an uninstall needs to clean beyond the file the MSI owns.
Shape 2: pass an activation key as an MSI property
Some deployments are licensed per seat, not per site, and the admin has a key rather than a signed file. Here you do not want to activate during the install — an online activation from a deferred custom action running as SYSTEM is the least reliable network call in your product, and it fingerprints the wrong security context. Instead, let the installer stage the key, and let the app activate itself on first run, in the user’s context, where it can reach the service and present the real machine identity.
Declare a public property so the admin can supply the key on the command line, and make it Secure so it survives the switch to the deferred (SYSTEM) execution phase:
<Property Id="LICENSEKEY" Secure="yes" />
<!-- Deferred CA needs its data marshalled in via a same-named property. -->
<SetProperty Id="StageLicenseKey" Value="[LICENSEKEY]"
Before="StageLicenseKey" Sequence="execute"
Condition="LICENSEKEY" />
<CustomAction Id="StageLicenseKey" BinaryRef="AcmeCa"
DllEntry="StageLicenseKey" Execute="deferred"
Impersonate="no" Return="check" />
<InstallExecuteSequence>
<Custom Action="StageLicenseKey" After="InstallFiles" Condition="LICENSEKEY" />
</InstallExecuteSequence>
The custom action writes the key to a well-known, machine-wide hand-off file. It deliberately does no networking — it only drops a breadcrumb the app will consume:
// WiX v5 custom action (WixToolset.Dtf.WindowsInstaller)
using WixToolset.Dtf.WindowsInstaller;
public class CustomActions
{
[CustomAction]
public static ActionResult StageLicenseKey(Session session)
{
// Deferred CAs cannot read properties directly; the value arrives as CustomActionData.
string key = session["CustomActionData"]?.Trim() ?? "";
if (key.Length == 0) return ActionResult.Success;
string dir = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
"Keyright", "acme-studio");
Directory.CreateDirectory(dir);
File.WriteAllText(Path.Combine(dir, "pending-activation.key"), key);
return ActionResult.Success;
}
}
On first launch the app looks for that breadcrumb, activates once, and deletes it so the activation never repeats:
static async Task EnsureActivatedAsync(KeyrightClient client)
{
// Already licensed from a cached lease? Nothing to do.
if (client.Validate().IsValid) return;
string pending = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
"Keyright", "acme-studio", "pending-activation.key");
if (!File.Exists(pending)) return; // no staged key; fall back to the UI's "Enter key" flow
string key = (await File.ReadAllTextAsync(pending)).Trim();
ActivationResult result = await client.ActivateAsync(key);
if (result.Status == LicenseStatus.Ok)
File.Delete(pending); // consumed; do not re-activate on next launch
else if (result.Status == LicenseStatus.SeatLimit)
File.Delete(pending); // a real answer, not a transient failure — stop retrying
// On a transient/offline failure, leave the file so the next launch retries.
}
The activation runs as the user, caches its lease per-user, and binds the seat to the machine the app actually runs on. Keep activation idempotent on your side (it already is in Keyright) so that two users on the same shared PC each consuming the same staged key refresh one seat rather than burning two — both present the same machine fingerprint, and the second ActivateAsync refreshes the first one’s device record.
The command lines an admin actually runs
Whatever you build, the output is a line someone pastes into a GPO, an Intune Win32 app, or an SCCM program. Document these next to the MSI:
:: Site license baked into the MSI — nothing to pass, fully silent, works offline.
msiexec /i Acme.Studio.msi /qn /l*v %TEMP%\acme-install.log
:: Per-seat: hand the installer the activation key. Quote it; keys can contain punctuation.
msiexec /i Acme.Studio.msi /qn LICENSEKEY="ACME-7F3A-9C21-5E08-BD44"
:: Intune Win32 app "install command" is the same line; detection rule = the app's EXE/version.
Two cautions worth putting in the admin’s hands. Property names passed on the command line must be public (all-caps) or MSI drops them — LICENSEKEY, never licenseKey. And a key on a command line can show up in the verbose MSI log and, briefly, in the process list; prefer the bundled-site-license shape for anything sensitive, and if you must pass a key, mark the property Secure and tell admins not to enable per-package /l*v logging in shared transcripts.
Uninstall, repair, and upgrade
The component that owns license.json makes the file’s lifecycle the MSI’s. A standard uninstall removes it, which is correct — the device is no longer licensed to run the product. A repair (msiexec /f) re-lays the shipped site license, healing a file a user deleted. A major upgrade replaces it with whatever the new MSI carries, so if you ship a fresh site license in a new build it supersedes the old one cleanly.
The staged activation key is different: it is not an MSI component (the installer writes it, it is not a shipped file), so an uninstall will not remove pending-activation.key unless you add a RemoveFile entry — do add one, so a reinstall does not re-activate against a key the customer has since rotated. The per-user lease the app cached is per-user AppData and outside the installer’s reach by design; if the customer is decommissioning the machine, have them deactivate in-app first, or free the device from the license’s Devices panel in the Keyright dashboard, the same as any seat move.
<Component Id="CleanStagedKey" Directory="AcmeProduct" Guid="*">
<RemoveFile Id="RmPendingKey" Name="pending-activation.key" On="uninstall" />
</Component>
What to take away
Enterprise software is installed by a tool and run by a stranger, so the license has to be in place before anyone launches the app. Put it where the device can read it — %ProgramData%, not per-user AppData — and let the MSI own its lifecycle. If the deployment is air-gapped or site-licensed, bundle a signed perpetual license and point DefaultLicensePath at it; the first run is licensed with no network and no dialog. If it is per-seat, stage the key from a deferred custom action and let the app activate itself on first run in the user’s own context, where the fingerprint and the network are real. Either way the person who turns the laptop on sees your product working, which is the only acceptable first run for software they did not buy and cannot activate.
Try Nebula.NET
Harden your .NET code in minutes — start with the free edition.