This sample demonstrates how to integrate license management into a WPF desktop application using the LicenseManagement.EndUser.Wpf package.
The sample shows:
- License validation at application startup
- Handling different license states (Valid, Trial, Expired)
- Feature gating based on license status
- Using the built-in license management UI
- Centralized license service pattern
WpfSampleApp.csproj- Project fileApp.xaml/App.xaml.cs- Application entry pointMainWindow.xaml/MainWindow.xaml.cs- Main application windowServices/LicenseService.cs- Centralized license management service
- .NET Framework 4.8.1
- LicenseManagement.EndUser.Wpf NuGet package (v2.0+)
- Account at license-management.com
From the License Management dashboard, note:
- Vendor ID:
VDR_01... - Product ID:
PRD_01... - Client API Key:
PUB_01...(NOT your Master key!) - Public Key: For offline validation
Edit Services/LicenseService.cs and replace the placeholder values:
private const string VendorId = "VDR_YOUR_VENDOR_ID";
private const string ProductId = "PRD_YOUR_PRODUCT_ID";
private const string ApiKey = "PUB_YOUR_CLIENT_API_KEY";
private const string PublicKey = @"<RSAKeyValue>
<Modulus>YOUR_MODULUS_HERE</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>";dotnet build WpfSampleApp.csproj- Application loads
MainWindow MainWindow_Loadedevent triggers license validationLicenseService.ValidateLicense()checks the local license file- UI updates based on the license status
| Status | Behavior |
|---|---|
Valid |
Full access to all features |
ValidTrial |
Limited features, trial countdown shown |
InValidTrial |
No access, prompt to purchase |
ReceiptExpired |
No access, prompt to renew |
ReceiptUnregistered |
No access, prompt to enter key |
// In your code, check license status before enabling features
if (_licenseService.HasProAccess())
{
// Enable pro features
EnableProFeature();
}
else if (_licenseService.IsTrialMode())
{
// Enable trial features only
EnableTrialFeatures();
}
else
{
// Show purchase prompt
ShowPurchaseDialog();
}The LicenseManagement.EndUser.Wpf package includes pre-built windows for:
- Viewing license details
- Registering a product key
- Unregistering the computer
// Show the license management window
var licenseWindow = new LicenseManagement.EndUser.Wpf.Views.MainWindow
{
License = LicenseViewModel.FromContext(context, products)
};
licenseWindow.ShowDialog();public class MainViewModel : INotifyPropertyChanged
{
private readonly LicenseService _licenseService;
public LicenseStatus Status => _licenseService.GetCachedContext()?.LicenseModel?.Status;
public bool CanAccessProFeatures => _licenseService.HasProAccess();
public ICommand ManageLicenseCommand { get; }
}// In App.xaml.cs or your DI setup
services.AddSingleton<LicenseService>();
// Inject into view models
public class MyViewModel
{
public MyViewModel(LicenseService licenseService)
{
_licenseService = licenseService;
}
}// For async validation (if your UI supports it)
public async Task ValidateLicenseAsync()
{
var context = new LicHandlingContext(preferences);
var handler = new LicenseHandlingLaunch(context);
await handler.HandleLicenseAsync();
}Instead of the built-in UI, create your own:
private void ShowCustomLicenseDialog()
{
var dialog = new MyCustomLicenseDialog();
if (dialog.ShowDialog() == true)
{
var receiptCode = dialog.ReceiptCode;
_licenseService.ActivateLicense(receiptCode);
ValidateLicense();
}
}The built-in views use resources from AppResources.xaml. Override styles in your App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/LicenseManagement.EndUser.Wpf;component/AppResources.xaml"/>
<!-- Your overrides -->
<ResourceDictionary>
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="#0078D7"/>
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>