How To: Call the Server API to Issue a License
With Microsoft Software Licensing and Protection Server 2008, you can issue licenses from templates programmatically to support scenarios where you want to automate the creation of multiple licenses. Integration with customer relationship management (CRM) software solutions becomes easier and more viable as a business solution with automated, programmatic licensing.
Note: Before starting the procedure, download the Microsoft Visual Studio 2005 extensions for .NET Framework 3.0, Windows Communication Foundation (WCF) and Windows Presentation Foundation (WPF), included in the November 2006 CTP add-in, and install them. If you have not installed the WCF and WPF extensions for the .NET Framework 3.0, you will not see the Add Service Reference menu item when you right-click References in Solution Explorer.
To Programmatically Issue a License from a Template
- Open your project in Microsoft Visual Studio 2005.
- Add a service reference to your solution:
- Right-click References in Solution Explorer.
- Click Add Service Reference.
- Paste an URL similar to the following one into the URL drop-down box, (substituting your server's domain for <mysrv.domain.com>). https://<mysrv.domain.com>/SLMServerWS/LicenseManagementWS.svc
- Click Go, and click Add Reference.
- Add the following Using Directives to your default form. Replace the last directive shown in the list with the namespace that was created when you added the reference earlier in Step 2.
Copy Code
using System.Net;
using System.Net.Security;
using System.ServiceModel;
using IssueLicenseByTemplateDemo.mysrv_domain_com;
- Create an appropriate namespace and class within your form.
Copy Code
public partial class MainForm : Form
{
// Add the code from the remaining steps here.
}
- Add code to validate the certificate returned from the SLP Server.
Copy Code
public static bool CertificatePolicyValidator(
Object sender,
System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
}
- Add the issueLicense method.
Copy Code
private void IssueLicense(string serverName,
string userName,
string password,
string templateLicenseKey,
string licenseDescription,
int numLicenses
)
- Register the certificate validation callback, create a public Web Service proxy, and issue a license by template.
Copy Code
{
try
{
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(CertificatePolicyValidator);
// Create public Web Service proxy
using (LicenseManagementWSClient LMClient =
CreateLicenseManagementClient(
serverName,
userName,
password))
{
// Issue a license by template for as many licenses requested
for (int i = 0; i < numLicenses && _cancelIssueLicense == false; i++)
{
string activationKey = LMClient.IssueLicenseByTemplate(
templateLicenseKey,
licenseDescription,
null);
AddToActivationKeys(activationKey);
}
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
- Create the Web Service client proxy.
Copy Code
private LicenseManagementWSClient CreateLicenseManagementClient(
string serverName,
string userName,
string password)
{
WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.TransportWithMessageCredential;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
binding.MessageEncoding = WSMessageEncoding.Text;
binding.TextEncoding = Encoding.UTF8;
string URL = "https://" + serverName + "/SLMServerWS/LicenseManagementWS.svc";
EndpointAddress ea = new EndpointAddress(URL);
LicenseManagementWSClient client = new LicenseManagementWSClient(binding, ea);
client.ClientCredentials.UserName.UserName = userName;
client.ClientCredentials.UserName.Password = password;
return client;
}
About the Web Service API
Currently, there is one public SLP Server API method, IssueLicenseByTemplate. This API has the following characteristics.
LicenseManagementWS.svc Web Service: IssueLicenseByTemplate
This method allows you to issue a license according to a specified template. It requires authentication and the permissions necessary to issue a license.
Syntax
Copy Code
string IssueLicenseByTemplate(
string templateLicenseKey,
string licenseDescription,
CustomTag[] customTags);
Parameters
templateLicenseKey License key of a template license
licenseDescription Description of a license. If empty, the description from the template will be used.
customTags List of custom name/value pairs that will be associated with the issued license. Can be null.
Exception
cref="System.Web.Services.Protocols.SoapException"
Returns
The license key of the newly issued license.
Example
Copy Code
using System.Net;
using System.Net.Security;
using System.ServiceModel;
using IssueLicenseByTemplateDemo.mysrv_domain_com;
namespace IssueLicenseByTemplateDemo
{
public partial class MainForm : Form
{
//…UI related code
public static bool CertificatePolicyValidator(
Object sender,
System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
}
private void IssueLicense(
string serverName,
string userName,
string password,
string templateLicenseKey,
string licenseDescription,
int numLicenses
)
{
try
{
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(CertificatePolicyValidator);
// Create public Web Service proxy
using (LicenseManagementWSClient LMClient =
CreateLicenseManagementClient(
serverName,
userName,
password))
{
// Issue a license by template for as many licenses requested
for (int i = 0; i < numLicenses && _cancelIssueLicense == false; i++)
{
string activationKey = LMClient.IssueLicenseByTemplate(
templateLicenseKey,
licenseDescription,
null);
AddToActivationKeys(activationKey);
}
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
private LicenseManagementWSClient CreateLicenseManagementClient(
string serverName,
string userName,
string password)
{
WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.TransportWithMessageCredential;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
binding.MessageEncoding = WSMessageEncoding.Text;
binding.TextEncoding = Encoding.UTF8;
string URL = "https://" + serverName + "/SLMServerWS/LicenseManagementWS.svc";
EndpointAddress ea = new EndpointAddress(URL);
LicenseManagementWSClient client = new LicenseManagementWSClient(binding, ea);
client.ClientCredentials.UserName.UserName = userName;
client.ClientCredentials.UserName.Password = password;
return client;
}
}
}
What do you think about this topic? Send feedback!
|