Custom License Storage
While a default location is predefined for the local license file, the flexibility to modify its location exists within LicenseSpring's C++ and .NET SDK.
This versatility allows for storing the license file in various alternative locations, including databases, Windows registries, or remote servers.
Prerequisites
Completed the Getting Started tutorial
You should have completed the Getting Started tutorial, specifically:
Initialized
LicenseManager(orLicenseHandler) with your configuration using the appropriate settings.Created a
LicenseIDusing eitherLicenseID::fromKeyorLicenseID::fromUserfunction, depending on the activation method you prefer.Implemented basic license management, including license activations, deactivations, and both online and local license checks.
Default License File Encryption
In the LicenseStorage interface, the encryption and decryption of the license file are executed by invoking basic functions within the C++ and .NET SDK.
Encryption takes place during the activation and initial storage of the license, utilizing the activateLicense() method on the LicenseManager object.
Decryption of the license file is done automatically when the SDK automatically reloads the license file, or when either reloadLicense() or getCurrentLicense() are called on the LicenseManager object.
Adding License File to a Database
Once you've established the derived class and successfully implemented the loadLicense(), saveLicense(), and clear() methods, the next step involves customizing the default file location.
To accomplish this, when instantiating the LicenseManager object, you should provide a second parameter. This additional parameter allows you to specify a different storage location for the local license.
By default, the local license is stored in a file. To check this default location, see Local License File.
In the .NET SDK, custom storage is supplied by implementing the ILicenseStorage interface and passing an instance to Initialize. The interface has three methods — SaveLicense(string), LoadLicense(), and Clear() — that receive and return the already-encrypted license data string, so your code only handles persistence, never cryptography.
Pass your storage to the manager when initializing:
Any exception thrown from these methods is wrapped in a LocalLicenseException and propagated.
Just changing the file location? You don't need a custom class. The default LicenseFileStorage lets you change the location of the file:
Methods to extend
Method
What super() does
What you add
save_licensefile(data_obj)
encrypts the license data (and merges bundles) and writes the .key file
save the encrypted string to the database
get_licensefile_dict(ignore_file_not_found=False)
reads the .key file, decrypts it, raises LicenseDeleted / LicenseFileCorruption
load the encrypted string from the database first
delete_licensefile()
removes the .key file and resets the in-memory cache
delete the record from the database
load_licensefile(), clear_storage() and encrypt()/decrypt() call these methods internally, so they don't need to be overridden.
The database is the source of truth. The local .key file is only a working copy: it is overwritten from the database on every load and removed when the database has no license.
Step 1: Create your DataStorage class
Example with SQLite. For another database, replace the three _db_* methods.
self._filename(thefilenamefrom Configuration,"License"by default) is used as the database key. To store several products in one table, use a differentfilenameper product._db_savemust overwrite the existing record. It runs on every activation, check, consumption sync and so on._db_loadreturnsNonewhen nothing is stored. The SDK then raisesLicenseDeleted, the same as for a missing.keyfile.Store the string unchanged. It is already encrypted.
Exceptions from the database (connection errors, permission errors, ...) propagate to the caller of the SDK method.
Step 2: Use it in LicenseManager
LicenseManager creates the default DataStorage in its constructor and in reconfigure. Replace it in both places:
The rest of the LicenseManager and License API works unchanged.
License File Terminology
LicenseFileStorageBase
LicenseFileStorageBase serves as the foundational class responsible for storing licenses in a file and implementing the LicenseStorage interface. It is important to note that this class does not incorporate any mechanisms for ensuring synchronized access to files, rendering it unsuitable for concurrent multi-threaded operations.
LicenseFileStorage
Derived from LicenseFileStorageBase, the LicenseFileStorage class inherits its characteristics and functionality, but with the added feature of thread-safe file operations. This class stands as the default choice for license storage, as it guarantees safe file handling even in multi-threaded scenarios.
LicenseFileStorageEx (deprecated in v8.0.0)
A further specialization of the LicenseFileStorageBase class is LicenseFileStorageEx. This advanced license file storage variant is specifically designed for interprocess applications. It employs a synchronization mechanism using a mutex provided by the Boost library, ensuring both thread and process safety. This class is ideal for scenarios where concurrent access from multiple threads and processes is required while maintaining the integrity of file operations.
LicenseFileStorageEx is deprecated in v8.0.0 because thread-safety is now built into LicenseFileStorage and optional process-safety is implemented in the SDK transaction layer (see Process Safety below). If you were using LicenseFileStorageEx to coordinate license access between multiple processes, you must explicitly enable process safety.
C# / .NET storage types
ILicenseStorage
Interface for custom storage (SaveLicense / LoadLicense / Clear).
LicenseFileStorage
Default. Stores the encrypted license in a file. Thread-safe.
LicenseMemoryStorage
Keeps the license in memory only. This is the default when ExtendedOptions.WebServiceMode is set.
LicenseFileStorageEx
Obsolete. Superseded by ExtendedOptions.EnableProcessSafety on the default LicenseFileStorage.
Process Safety
Starting in C++ SDK v8.0.0, the SDK can serialize license file reads and writes across multiple processes. This is disabled by default because most applications only access the license file from a single process, and the locking adds I/O overhead on every read and write.
Enable it through ExtendedOptions:
When EnableProcessSafety is enabled, the SDK serializes cross-process reads and writes using a companion <license_file>.lock file (FileShare.None on Windows, flock on Unix) and performs an atomic read-modify-write — it re-reads the latest file contents under the lock before applying each change, avoiding lost updates. When disabled (the default), the SDK assumes a single writer and serves reads from an in-memory cache for performance.
When enabled, the SDK acquires an exclusive OS-level lock on <license_file>.lock (e.g. License.key.lock) for the duration of every read-modify-write transaction, and re-reads the license file on every access to detect changes made by other processes. The lock file is created next to the license file the first time it is needed and doesn't get deleted later on. The kernel releases the file lock automatically if a process exits or crashes while holding it.
When to enable it
You run multiple processes that all activate, check and use the same license file.
You share the license file across two different LicenseSpring SDKs (e.g. C++ and .NET) on the same machine.
When to leave it disabled (the default)
A single process accesses the license file.
You implement a custom
LicenseStoragethat has its own concurrency control (e.g. a database).
Last updated
Was this helpful?