How to implement license control similar to AutoCount
Posted on November 3, 2014 | By Mohd Imran
The following is a sample code on how to implement license control similar to AutoCount.
License Control Code Snippet
- namespace Example
- {
- public class SamplePlugIn : BCE.AutoCount.PlugIn.BasePlugIn
- {
- public static bool GlobalIsRegistered = false;
- public SamplePlugIn()
- : base(newGuid(“97814787-C48A-4E8E-8549-994850141A09″), “AutoCount Accounting Sample Plug-Ins”, “1.0″)
- {
- // add code to complete the constructor
- SetSupportLicenseKey(true);
- }
- public override void UpdateLicenseStatus(BCE.AutoCount.PlugIn.LicenseStatusArgs e)
- {
- // place code here to notify your plug-in that your plug-in has registered
- if (e.LicenseStatus == BCE.AutoCount.PlugIn.LicenseStatus.PermanentLicense)
- GlobalIsRegistered = true;
- else
- GlobalIsRegistered = false;
- }
- public override void TestLicenseKey(BCE.AutoCount.PlugIn.LicenseStatusArgs e, string licenseKey)
- {
- try
- {
- bool isRegistered;
- // if client uses dongle
- if (e.DongleSerialNumber != null)
- isRegistered = CompareKey(e.Guid, e.DongleSerialNumber, licenseKey);
- else// if not using dongle
- isRegistered = CompareKey(e.Guid, e.CompanyName, e.Address1, licenseKey);
- if (!isRegistered)
- e.LicenseStatus = BCE.AutoCount.PlugIn.LicenseStatus.Unregistered;
- else
- e.LicenseStatus = BCE.AutoCount.PlugIn.LicenseStatus.PermanentLicense;
- }
- catch (Exception ex)
- {
- // capture error message
- }
- }
- // This is an example please do not use this to create your own key generation/comparison
- public bool CompareKey(Guid guid, byte[] dongleSerial, string licenseKey)
- {
- StringBuilder sb = newStringBuilder();
- for (int i = 0; i < dongleSerial.Length; i++)
- {
- sb.Append(dongleSerial[i].ToString(“X2″));
- }
- string dongleSN = sb.ToString();
- string uniqueSerial = guid.ToString(“N”);
- string combiSerial = dongleSN + uniqueSerial;
- string uniqueSerialLength = combiSerial.Substring(0, 28).ToUpper();
- char[] serialArray = uniqueSerialLength.ToCharArray();
- string finalSerialNo = “”;
- int j = 0;
- for (int i = 0; i < 28; i++)
- {
- for (j = i; j < 4 + i; j++)
- {
- finalSerialNo += serialArray[j];
- }
- if (j == 28)
- {
- break;
- }
- else
- {
- i = (j) – 1;
- finalSerialNo += “-”;
- }
- }
- return finalSerialNo.Equals(licenseKey);
- }
- // This is an example please do not use this to create your own key generation/comparison
- public bool CompareKey(Guid guid, string companyName, string address1, string licenseKey)
- {
- string uniqueSerial = guid.ToString(“N”);
- string uniqueSerialLength = uniqueSerial.Substring(0, 10).ToUpper();
- char[] serialArray = uniqueSerialLength.ToCharArray();
- char[] companyArray = RemoveSpecialCharacters(companyName.ToUpper());
- char[] addressArray = RemoveSpecialCharacters(address1.ToUpper());
- char[] finalArray = newchar[serialArray.Length + companyArray.Length + addressArray.Length];
- for (int i = 0; i < 28; i++)
- {
- switch (i)
- {
- case (0): Array.Copy(companyArray, 0, finalArray, 0, 2); break;
- case (2): Array.Copy(serialArray, 0, finalArray, 2, 2); break;
- case (4): Array.Copy(addressArray, 0, finalArray, 4, 2); break;
- case (6): Array.Copy(companyArray, 2, finalArray, 6, 2); break;
- case (8): Array.Copy(serialArray, 2, finalArray, 8, 2); break;
- case (10): Array.Copy(addressArray, 2, finalArray, 10, 2); break;
- case (12): Array.Copy(companyArray, 4, finalArray, 12, 2); break;
- case (14): Array.Copy(serialArray, 4, finalArray, 14, 2); break;
- case (16): Array.Copy(addressArray, 4, finalArray, 16, 2); break;
- case (18): Array.Copy(companyArray, 6, finalArray, 18, 3); break;
- case (21): Array.Copy(addressArray, 6, finalArray, 21, 3); break;
- case (24): Array.Copy(serialArray, 6, finalArray, 24, 4); break;
- }
- }
- string finalSerialNo = “”;
- int j = 0;
- for (int i = 0; i < 28; i++)
- {
- for (j = i; j < 4 + i; j++)
- {
- finalSerialNo += finalArray[j];
- }
- if (j == 28)
- {
- break;
- }
- else
- {
- i = (j) – 1;
- finalSerialNo += “-”;
- }
- }
- return finalSerialNo.Equals(licenseKey);
- }
- // remove special character from address and company name
- public char[] RemoveSpecialCharacters(string str)
- {
- char[] buffer = newchar[str.Length];
- int idx = 0;
- foreach (char c in str)
- {
- // since using to upper only care about numbers and upper case
- if ((c >= ’0′ && c <= ’9′) || (c >= ‘A’ && c <= ‘Z’))
- {
- buffer[idx] = c;
- idx++;
- }
- }
- return buffer.Take(9).ToArray();
- }
- }
- }