суббота, 5 января 2013 г.

Using of the custom profile to activate license against e-Mail address


Often our customers ask whether it is possible to use e-Mail address instead of PC hardware information for license activation. I would say that from my point of view using of the e-Mail address isn’t as secure as using of the hardware profile, but answer is “yes, you can use e-Mail address for license activation”. In this post I’ll describe how you can use “Custom Profile” feature for these purposes.
I suppose you’ve read following topics in the Manco .NET Licensing System documentation:

1.       Quick start using “Unlock Key” licensing schema.
2.       Online product activation.
3.       Include custom information to the PC profile.
Product documentation is a part of the installation package (you can get it here: http://www.mancosoftware.com/licensing/download.htm). After installation you can find product documentation in the Windows Start menu at the “All Programs->Manco Software->Licensing System”, file name is “Manco .NET Licensing System – Documentation.pdf”. It contains about 220 pages with different aspects of using of our system.

Using “Custom Profile” feature you can add custom information to the PC profile, or even completely replace it with your own value. To do it you have to use CustomProfileMode and CustomProfile properties of the license object. The CustomProfileMode property sets value which indicates how the custom profile will be combined with system information. It can have following values:

  1. None – indicates that custom profile will not be used.Combine – indicates that custom profile will be added to the hardware information.
  2. Combine – indicates that custom profile will be added to the hardware information.
  3. Override – indicates that custom profile will override hardware information.
 
The CustomProfile property sets value which will be used as custom profile.
Since we would like to use customer’s e-Mail address for license activation we should override PC system information with e-Mail address. So we should set CustomProfileMode to the “Override” and put e-Mail address to the CustomProfile. We also should provide an application with ability to store entered e-Mail address. We can use license custom value for these purposes.

Here are the changes we should make in the basic code of the “Unlock Key with Activation” licensing schema to be able to activate license against e-Mail address:

[C#]
public MainWindow()
{
       InitializeComponent();
 
       // Instantiate license object and assign assemblies for validation.
       this.license = (Manco.Licensing.License)LicenseManager.Validate(
             typeof(MainWindow), this);
       this.license.LicensedAssembly = typeof(MainWindow).Assembly;
 
       // We will override PC profile with custom value.
       this.license.CustomProfileMode = CustomProfileMode.Override;
 
       // Read e-Mail address from the custom license value
       // and use it as custom PC profile.
       this.license.CustomProfile = this.license.GetCustomValue("EMailAddress");
 
       this.licenseProperties = this.license.GetLicenseProperties();
       this.licenseState = this.license.GetLicenseState(true);
}
 
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
       // Check if the current license is evaluation license or not valid
       if (this.licenseState.IsEvaluation || !this.licenseState.IsValid)
       {
             // This is evaluation or not valid license,
             // so we should show evaluation dialog
 
             // Clear saved keys to allow enter all keys from the scratch.
             license.ClearUnlockKeys();
 
             EvaluationWindow loForm = new EvaluationWindow(license);
             loForm.Owner = this;
             if (loForm.ShowDialog() == true
                    && loForm.UnlockKey.Trim() != string.Empty
                    && loForm.ActivationKey.Trim() != string.Empty)
             {
                    // Check whether license REQUIRE the calling of the AWS
                    // and it has been called from the license form.
                    if (loForm.IsAwsCalled || !this.licenseProperties.DoForceValidation)
                    {
                           // License information have been entered.
                           // Pass it to the license object for the
                           // following validation.
                           license.UnlockKey = loForm.UnlockKey;
                           license.ActivationKey = loForm.ActivationKey;
 
                           // Save entered e-Mail address for the future using.
                           this.license.SetCustomValue("EMailAddress",
                                  loForm.EMailAddress);
                    }
                    else
                    {
                           MessageBox.Show("You must click 'Activate' button on the valuation form"
                                 + "to activate your copy of the product on this PC.");
                    }

             }
             else if (this.licenseState.IsEvaluationExpired)
             {
                    // The evaluation license has been expired and
                    // no license information have been entered,
                    // so we should close application.
                    Application.Current.Shutdown();
                    return;
             }
       }
 
       // ...
}
 
private void ActivateLicense(object sender, RoutedEventArgs e)
{
       // If "Unlock Key" has been entered
       if (!string.IsNullOrEmpty(this.UnlockKey)
             && !string.IsNullOrEmpty(this.EMailAddress))
       {
             // Pass "Unlock Key" to the license object
             this.license.UnlockKey = this.UnlockKey;
 
             // Pass e-Mail address as custom profile.
             this.license.CustomProfile = this.EMailAddress;
 
             // Get instance of the Activation Service
             TimeSpan loAWSTimeout = TimeSpan.FromSeconds(120);
             ActivationWebService.ActivationServiceSoapClient loAWSClient =
                    AwsHelper.InitializeAwsClient(
                    "http://localhost/ActivationService/ActivationService.asmx",
                    loAWSTimeout,
                    loAWSTimeout);
             try
             {
                    bool isActivated;
 
                    if (loAWSClient.LicenseExists(this.license.ProductID, out isActivated))
                    {
                           // Get product ID from the license and try to activate it
                           // Instruct AWS do NOT send copy of the key to the
                           // customer's e-Mail
                           string lsActivationKey =
                                  loAWSClient.ActivateProductGetKey(
this.license.ProductID, false);
                           if (lsActivationKey != null)
                           {
                                  // AWS has been called
                                  m_bAWSCalled = true;
                                  txtActivationKey.Text = lsActivationKey;
                                  MessageBox.Show("Activation Key has been succesfully"
                                        + " generated and passed to the text box."
                                        + " Click OK to continue.");
                           }
                    }
             }
             catch (SoapException exc)
             {
                    if (exc.Message.Contains("Manco.Licensing.ActivationWebService.Exceptions.AllowedActivationsExceededException"))
                    {
                           // Process AllowedActivationsExceededException
                           MessageBox.Show("Number of the allowed activations exceeded.");
                    }
                    else
                    {
                           MessageBox.Show("Exception during activation:\n" + exc.ToString));
                    }
             }
             catch (Exception exc)
             {
                    MessageBox.Show("Exception during activation:\n" + exc.ToString());
             }
       }
       else
       {
             MessageBox.Show("Enter Unlock Key and e-Mail address to be able activate product");
       }
}
 

[VB.NET]
Public Sub New()
       InitializeComponent()
 
       ' Instantiate license object and assign assemblies for validation.
       Me.license = DirectCast(LicenseManager.Validate(GetType(MainWindow), Me), Manco.Licensing.License)
       Me.license.LicensedAssembly = GetType(MainWindow).Assembly

       ' We will override PC profile with custom value.
       Me.license.CustomProfileMode = CustomProfileMode.Override

       ' Read e-Mail address from the custom license value
       ' and use it as custom PC profile.
       Me.license.CustomProfile = Me.license.GetCustomValue("EMailAddress")
 
       Me.licenseProperties = Me.license.GetLicenseProperties()
       Me.licenseState = Me.license.GetLicenseState(True)
End Sub
 
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs)
       ' Check if the current license is evaluation license or not valid
       If Me.licenseState.IsEvaluation OrElse Not Me.licenseState.IsValid Then
             ' This is evaluation or not valid license,
             ' so we should show evaluation dialog
 
             ' Clear saved keys to allow enter all keys from the scratch.
             license.ClearUnlockKeys()
 
             Dim loForm As New EvaluationWindow(license)
             loForm.Owner = Me
             If loForm.ShowDialog() = True _
                    AndAlso loForm.UnlockKey.Trim() <> String.Empty _
                    AndAlso loForm.ActivationKey.Trim() <> String.Empty Then
                    ' Check whether license REQUIRE the calling of the AWS
                    ' and it has been called from the license form.
                    If loForm.IsAwsCalled _
                     OrElse Not Me.licenseProperties.DoForceValidation Then
                           ' License information have been entered.
                           ' Pass it to the license object for the
                           ' following validation.
                           license.UnlockKey = loForm.UnlockKey
                           license.ActivationKey = loForm.ActivationKey
 
                           ' Save entered e-Mail address for the future using.
                           Me.license.SetCustomValue("EMailAddress", loForm.EMailAddress)
                    Else
                           MessageBox.Show("You must click 'Activate' button on the evaluation form" _
                                  & "to activate your copy of the product on this PC.")
                    End If
             ElseIf Me.licenseState.IsEvaluationExpired Then
                    ' The evaluation license has been expired and
                    ' no license information have been entered,
                    ' so we should close application.
                    Application.Current.Shutdown()
                    Return
             End If
       End If
 
       ' ...
End Sub

Private Sub ActivateLicense(sender As Object, e As RoutedEventArgs)
       ' If "Unlock Key" has been entered
       If Not String.IsNullOrEmpty(Me.UnlockKey) _
              AndAlso Not String.IsNullOrEmpty(Me.EMailAddress) Then
             ' Pass "Unlock Key" to the license object
             Me.license.UnlockKey = Me.UnlockKey
 
             ' Pass e-Mail address as custom profile.
             Me.license.CustomProfile = Me.EMailAddress
 
             ' Get instance of the Activation Service
             Dim loAWSTimeout As TimeSpan = TimeSpan.FromSeconds(120)
             Dim loAWSClient As ActivationWebService.ActivationServiceSoapClient = _
                    AwsHelper.InitializeAwsClient( _
                    "http://localhost/ActivationService/ActivationService.asmx", _
                    loAWSTimeout, _
                    loAWSTimeout)
             Try
                    Dim isActivated As Boolean
                    If loAWSClient.LicenseExists(Me.license.ProductID, isActivated) Then
                           ' Get product ID from the license and try to activate it
                           ' Instruct AWS do NOT send copy of the key to the
                           ' customer's e-Mail
                           Dim lsActivationKey As String = loAWSClient.ActivateProductGetKey(Me.license.ProductID, False)
                           If lsActivationKey IsNot Nothing Then
                                  ' AWS has been called
                                  m_bAWSCalled = True
                                  txtActivationKey.Text = lsActivationKey
                                  MessageBox.Show("Activation Key has been succesfully" _
                                  & " generated and passed to the text box." _
                                  & " Click OK to continue.")
                           End If
                    End If
             Catch exc As SoapException
                    If exc.Message.Contains("Manco.Licensing.ActivationWebService.Exceptions.AllowedActivationsExceededException") Then
                           ' Process AllowedActivationsExceededException
                           MessageBox.Show("Number of the allowed activations exceeded.")
                    Else
                           MessageBox.Show("Exception during activation:" _
                                  & vbLf & exc.ToString())
                    End If
             Catch exc As Exception
                    MessageBox.Show("Exception during activation:" _
                                  & vbLf & exc.ToString())
             End Try
       Else
             MessageBox.Show("Enter Unlock Key and e-Mail address to be able activate product")
       End If
End Sub
 
You can find sample solution which demonstrates how the custom profile can be used to activate license against e-Mail address here: http://www.mancosoftware.com/licensing/download.htm