Search website

How to implement license control similar to AutoCount

The following is a sample code on how to implement license control similar to AutoCount.

License Control Code Snippet
  1. namespace Example
  2. {
  3.     public class SamplePlugIn : BCE.AutoCount.PlugIn.BasePlugIn
  4.     {        
  5.         public static bool GlobalIsRegistered = false;
  6.         public SamplePlugIn()
  7.             : base(newGuid(“97814787-C48A-4E8E-8549-994850141A09″), “AutoCount Accounting Sample Plug-Ins”, “1.0″)
  8.         {
  9.             // add code to complete the constructor
  10.             SetSupportLicenseKey(true);
  11.         }
  12.         public override void UpdateLicenseStatus(BCE.AutoCount.PlugIn.LicenseStatusArgs e)
  13.         {
  14.             // place code here to notify your plug-in that your plug-in has registered
  15.             if (e.LicenseStatus == BCE.AutoCount.PlugIn.LicenseStatus.PermanentLicense)
  16.                 GlobalIsRegistered = true;
  17.             else
  18.                 GlobalIsRegistered = false;
  19.         }
  20.         public override void TestLicenseKey(BCE.AutoCount.PlugIn.LicenseStatusArgs e, string licenseKey)
  21.         {
  22.             try
  23.             {
  24.                 bool isRegistered;
  25.                 // if client uses dongle
  26.                 if (e.DongleSerialNumber != null)
  27.                     isRegistered = CompareKey(e.Guid, e.DongleSerialNumber, licenseKey);
  28.                 else// if not using dongle
  29.                     isRegistered = CompareKey(e.Guid, e.CompanyName, e.Address1, licenseKey);
  30.                 if (!isRegistered)
  31.                     e.LicenseStatus = BCE.AutoCount.PlugIn.LicenseStatus.Unregistered;
  32.                 else
  33.                     e.LicenseStatus = BCE.AutoCount.PlugIn.LicenseStatus.PermanentLicense;
  34.             }
  35.             catch (Exception ex)
  36.             {
  37.                 // capture error message
  38.             }
  39.         }
  40.         // This is an example please do not use this to create your own key generation/comparison
  41.         public bool CompareKey(Guid guid, byte[] dongleSerial, string licenseKey)
  42.         {
  43.             StringBuilder sb = newStringBuilder();
  44.             for (int i = 0; i < dongleSerial.Length; i++)
  45.             {
  46.                 sb.Append(dongleSerial[i].ToString(“X2″));
  47.             }
  48.             string dongleSN = sb.ToString();
  49.             string uniqueSerial = guid.ToString(“N”);            
  50.             string combiSerial = dongleSN + uniqueSerial;
  51.             string uniqueSerialLength = combiSerial.Substring(0, 28).ToUpper();
  52.             char[] serialArray = uniqueSerialLength.ToCharArray();
  53.             string finalSerialNo = “”;
  54.             int j = 0;
  55.             for (int i = 0; i < 28; i++)
  56.             {
  57.                 for (j = i; j < 4 + i; j++)
  58.                 {
  59.                     finalSerialNo += serialArray[j];
  60.                 }
  61.                 if (j == 28)
  62.                 {
  63.                     break;
  64.                 }
  65.                 else
  66.                 {
  67.                     i = (j) – 1;
  68.                     finalSerialNo += “-”;
  69.                 }
  70.             }
  71.             return finalSerialNo.Equals(licenseKey);
  72.         }
  73.         // This is an example please do not use this to create your own key generation/comparison
  74.         public bool CompareKey(Guid guid, string companyName, string address1, string licenseKey)
  75.         {
  76.             string uniqueSerial = guid.ToString(“N”);
  77.             string uniqueSerialLength = uniqueSerial.Substring(0, 10).ToUpper();
  78.             char[] serialArray = uniqueSerialLength.ToCharArray();
  79.             char[] companyArray = RemoveSpecialCharacters(companyName.ToUpper());
  80.             char[] addressArray = RemoveSpecialCharacters(address1.ToUpper());
  81.             char[] finalArray = newchar[serialArray.Length + companyArray.Length + addressArray.Length];
  82.             for (int i = 0; i < 28; i++)
  83.             {
  84.                 switch (i)
  85.                 {
  86.                     case (0): Array.Copy(companyArray, 0, finalArray, 0, 2); break;
  87.                     case (2): Array.Copy(serialArray, 0, finalArray, 2, 2); break;
  88.                     case (4): Array.Copy(addressArray, 0, finalArray, 4, 2); break;
  89.                     case (6): Array.Copy(companyArray, 2, finalArray, 6, 2); break;
  90.                     case (8): Array.Copy(serialArray, 2, finalArray, 8, 2); break;
  91.                     case (10): Array.Copy(addressArray, 2, finalArray, 10, 2); break;
  92.                     case (12): Array.Copy(companyArray, 4, finalArray, 12, 2); break;
  93.                     case (14): Array.Copy(serialArray, 4, finalArray, 14, 2); break;
  94.                     case (16): Array.Copy(addressArray, 4, finalArray, 16, 2); break;
  95.                     case (18): Array.Copy(companyArray, 6, finalArray, 18, 3); break;
  96.                     case (21): Array.Copy(addressArray, 6, finalArray, 21, 3); break;
  97.                     case (24): Array.Copy(serialArray, 6, finalArray, 24, 4); break;
  98.                 }
  99.             }
  100.             string finalSerialNo = “”;
  101.             int j = 0;
  102.             for (int i = 0; i < 28; i++)
  103.             {
  104.                 for (j = i; j < 4 + i; j++)
  105.                 {
  106.                     finalSerialNo += finalArray[j];
  107.                 }
  108.                 if (j == 28)
  109.                 {
  110.                     break;
  111.                 }
  112.                 else
  113.                 {
  114.                     i = (j) – 1;
  115.                     finalSerialNo += “-”;
  116.                 }
  117.             }
  118.             return finalSerialNo.Equals(licenseKey);
  119.         }
  120.         // remove special character from address and company name
  121.         public char[] RemoveSpecialCharacters(string str)
  122.         {
  123.             char[] buffer = newchar[str.Length];
  124.             int idx = 0;
  125.             foreach (char c in str)
  126.             {
  127.                 // since using to upper only care about numbers and upper case
  128.                 if ((c >= ’0′ && c <= ’9′) || (c >= ‘A’ && c <= ‘Z’))
  129.                 {
  130.                     buffer[idx] = c;
  131.                     idx++;
  132.                 }
  133.             }
  134.             return buffer.Take(9).ToArray();
  135.         }
  136.     }
  137. }