This class provides methods for configuring the service, requesting and releasing licenses, activating and deactivating licenses, and validating licenses.
Constructors
BabelLicensing()
BabelLicensing class constructor
publicBabelLicensing()
BabelLicensing(BabelLicensingConfiguration)
This is the constructor for the BabelLicensing class that takes a configuration object as a parameter. It initializes a new instance of the BabelLicensing class and sets the configuration property to the provided configuration object.
// Create a new configuration objectBabelLicensingConfiguration config =newBabelLicensingConfiguration() { // Set the service URL ServiceUrl ="http://localhost:5005", // Set the public key used to verify the license signature // See RSASignature for more information on how to export a public key SignatureProvider =RSASignature.FromKeys("public key here"), // Set a unique client ID ClientId ="my custom client id"};// Create the client object used to communicate with the serverBabelLicensing client =newBabelLicensing(config);
' Create a new configuration object
Dim config As New BabelLicensingConfiguration() With {
' Set the service URL
.ServiceUrl = "http://localhost:5005",
' Set the public key used to verify the license signature
' See RSASignature for more information on how to export a public key
.SignatureProvider = RSASignature.FromKeys("public key here"),
' Set a unique client ID
.ClientId = "my custom client id"
}
' Create the client object used to communicate with the server
Dim client As New BabelLicensing(config)
usingBabel.Licensing;// Create a new configuration objectBabelLicensingConfiguration config =newBabelLicensingConfiguration() { // Set the service URL ServiceUrl ="http://localhost:5005", // Set the public key used to verify the license signature SignatureProvider =RSASignature.FromKeys("<public key>"), // Set the application name // This will identify the application on the and server ClientId ="<application name>"};// Create the client object used to communicate with the serverBabelLicensing client =newBabelLicensing(config);// Use BabelServiceLicenseProvider to cache a local copy of the license for offline useBabelServiceLicenseProvider provider =newBabelServiceLicenseProvider(client) { // Refresh the license contacting the server every 5 days // If set to TimeSpan.Zero, the license will be validated every time on the server LicenseRefreshInterval =TimeSpan.FromDays(5)};// Register the license provider with BabelLicenseManagerBabelLicenseManager.RegisterLicenseProvider(typeof(Program), provider);// Check if the user key is setif (provider.UserKey==null){ // If the user key is not set, the license has not been activated yet. // Ask the user to activate the licenseConsole.WriteLine("Please enter your activation license key: ");string? userKey =Console.ReadLine();try {var result =awaitclient.ActivateLicenseAsync(userKey,typeof(Program)); Console.WriteLine($"License {result.License.Id} activated, client ({result.ActiveClientCount}/{result.MaxClientCount})");
}catch (Exception ex) {Console.WriteLine($"Could not activate license: {ex.Message}");return; }}try{ // Validate the license // This will contact the server to validate the license if the local copy is expired // You can customize the validation interval using the LicenseValidationInterval propertyvar license =awaitBabelLicenseManager.ValidateAsync(typeof(Program));Console.WriteLine($"License {license.Id} valid."); // Ask to deactivate the licenseConsole.WriteLine("Do you want to deactivate the license? (y/n)");string? answer =Console.ReadLine();if (answer?.ToLower() =="y") {try {awaitclient.DeactivateLicenseAsync(provider.UserKey);Console.WriteLine("License deactivated."); }catch (Exception ex) {Console.WriteLine($"Could not deactivate license: {ex.Message}"); } }}catch (Exception ex){Console.WriteLine($"License not valid: {ex.Message}");return;}
Imports Babel.Licensing
' Create a new configuration object
Dim config As New BabelLicensingConfiguration() With {
' Set the service URL
.ServiceUrl = "http://localhost:5005",
' Set the public key used to verify the license signature
.SignatureProvider = RSASignature.FromKeys("<public key>"),
' Set the application name
' This will identify the application on the and server
.ClientId = "<application name>"
}
' Create the client object used to communicate with the server
Dim client As New BabelLicensing(config)
' Use BabelServiceLicenseProvider to cache a local copy of the license for offline use
Dim provider As New BabelServiceLicenseProvider(client) With {
' Refresh the license contacting the server every 5 days
' If set to TimeSpan.Zero, the license will be validated every time on the server
.LicenseRefreshInterval = TimeSpan.FromDays(5)
}
' Register the license provider with BabelLicenseManager
BabelLicenseManager.RegisterLicenseProvider(GetType(Program), provider)
' Check if the user key is set
If provider.UserKey Is Nothing Then
' If the user key is not set, the license has not been activated yet.
' Ask the user to activate the license
Console.WriteLine("Please enter your activation license key: ")
Dim userKey As String = Console.ReadLine()
Try
Dim result = Await client.ActivateLicenseAsync(userKey, GetType(Program))
Console.WriteLine($"License {result.License.Id} activated, client ({result.ActiveClientCount}/{result.MaxClientCount})")
Catch ex As Exception
Console.WriteLine($"Could not activate license: {ex.Message}")
Return
End Try
End If
Try
' Validate the license
' This will contact the server to validate the license if the local copy is expired
' You can customize the validation interval using the LicenseValidationInterval property
Dim license = Await BabelLicenseManager.ValidateAsync(GetType(Program))
Console.WriteLine($"License {license.Id} valid.")
' Ask to deactivate the license
Console.WriteLine("Do you want to deactivate the license? (y/n)")
Dim answer As String = Console.ReadLine()?.ToLower()
If answer = "y" Then
Try
Await client.DeactivateLicenseAsync(provider.UserKey)
Console.WriteLine("License deactivated.")
Catch ex As Exception
Console.WriteLine($"Could not deactivate license: {ex.Message}")
End Try
End If
Catch ex As Exception
Console.WriteLine($"License not valid: {ex.Message}")
Return
End Try
BabelLicensing.Configure(config => { // Set the service URL ServiceUrl ="http://localhost:5005"; // To extract the public key from a .snk file use the following code // var rsa = RSASignature.CreateFromKeyFile(@"Keys.snk"); // string publicKey = rsa.ExportKeys(true);string publicKey ="public key here";config.SignatureProvider=RSASignature.FromKeys(publicKey); // Set client id to identify this applicationconfig.ClientId="my custom client id";});
BabelLicensing.Configure(Sub(config)
' Set the service URL
config.ServiceUrl = "http://localhost:5005"
' To extract the public key from a .snk file use the following code
' Dim rsa = RSASignature.CreateFromKeyFile("Keys.snk")
' Dim publicKey As String = rsa.ExportKeys(True)
Dim publicKey As String = "public key here"
config.SignatureProvider = RSASignature.FromKeys(publicKey)
' Set client id to identify this application
config.ClientId = "my custom client id"
End Sub)
CreateDefaultConfiguration()
Creates a default configuration for the Babel Licensing service.
usingBabel.Licensing;// Create a new configuration objectBabelLicensingConfiguration config =newBabelLicensingConfiguration() { // Set the service URL ServiceUrl ="http://localhost:5005", // Set the public key used to verify the license signature SignatureProvider =RSASignature.FromKeys("<public key>"), // Set a unique client ID for this application instance ClientId =Guid.NewGuid().ToString()};// Create the client object used to communicate with the serverBabelLicensing client =newBabelLicensing(config);// Ask the user to activate the licenseConsole.WriteLine("Please enter your floating license key: ");string? userKey =Console.ReadLine();try{var result =awaitclient.RequestFloatingLicenseAsync(userKey,typeof(Program));Console.WriteLine($"License {result.License.Id} requested.");Console.WriteLine($"There are {result.ActiveClientCount}/{result.MaxClientCount} clients active.");}catch (Exception ex){Console.WriteLine($"Could not request floating license: {ex.Message}");return;}try{ // Simulate a long task which requires the allocation of a licensefor (int i =0; i <10; i++) { // Validate the license // This will contact the server to validate the licensevar result =awaitclient.ValidateLicenseAsync(userKey,typeof(Program));Console.WriteLine($"{i}) License {result.License.Id} valid."); // Wait 1 secondawaitTask.Delay(1000); }try { // When the application is done with the license, it can release itawaitclient.ReleaseFloatingLicenseAsync(userKey);Console.WriteLine("License released."); }catch (Exception ex) {Console.WriteLine($"Could not release license: {ex.Message}"); }}catch (Exception ex){Console.WriteLine($"License not valid: {ex.Message}");return;}
Imports Babel.Licensing
' Create a new configuration object
Dim config As New BabelLicensingConfiguration() With {
' Set the service URL
.ServiceUrl = "http://localhost:5005",
' Set the public key used to verify the license signature
.SignatureProvider = RSASignature.FromKeys("<public key>"),
' Set a unique client ID for this application instance
.ClientId = Guid.NewGuid().ToString()
}
' Create the client object used to communicate with the server
Dim client As New BabelLicensing(config)
' Ask the user to activate the license
Console.WriteLine("Please enter your floating license key: ")
Dim userKey As String = Console.ReadLine()
Try
Dim result = Await client.RequestFloatingLicenseAsync(userKey, GetType(Program))
Console.WriteLine($"License {result.License.Id} requested.")
Console.WriteLine($"There are {result.ActiveClientCount}/{result.MaxClientCount} clients active.")
Catch ex As Exception
Console.WriteLine($"Could not request floating license: {ex.Message}")
Return
End Try
Try
' Simulate a long task which requires the allocation of a license
For i As Integer = 0 To 9
' Validate the license
' This will contact the server to validate the license
Dim result = Await client.ValidateLicenseAsync(userKey, GetType(Program))
Console.WriteLine($"{i}) License {result.License.Id} valid.")
' Wait 1 second
Await Task.Delay(1000)
Next
Try
' When the application is done with the license, it can release it
Await client.ReleaseFloatingLicenseAsync(userKey)
Console.WriteLine("License released.")
Catch ex As Exception
Console.WriteLine($"Could not release license: {ex.Message}")
End Try
Catch ex As Exception
Console.WriteLine($"License not valid: {ex.Message}")
Return
End Try