/// <summary> /// License validation /// </summary> public bool Validate() { licenseInfos.Clear(); // workaround for obfuscation var licenseTypes = new LicenseType[] { LicenseType.PMCTrial, LicenseType.PMCNormal }; // validate each possible type foreach (LicenseType licenseType in licenseTypes) { SecureLicense license = null; NoLicenseException validationException = null; var isExpired = false; var serial = string.Empty; var licenseManager = ServiceLocator.Current.GetInstance <ILicenseManager>(licenseType.ToString()); try { var requestInfo = new LicenseValidationRequestInfo { DontShowForms = true }; license = licenseManager.Validate(requestInfo); if (!license.IsTrial) { LogProvider.Log.Info(CustomModulesNames.PMCatcher, $"Found license: {license.SerialNumber.Substring(0, 3)}-*"); } } catch (NoLicenseException ex) { validationException = ex; var exceptionData = ParseException(ex); if (exceptionData != null && exceptionData.ContainsKey("errorCode") && exceptionData["errorCode"].Equals("LCS_EXP")) { isExpired = true; serial = exceptionData["serial"]; } } catch (Exception ex) { LogProvider.Log.Error(CustomModulesNames.PMCatcher, "Validation: License validation error", ex); } // Trial license - check if real trial is expired or does not exists if (license != null && license.IsTrial) { licenseManager.ResetCacheForLicense(license); var requestInfo = new LicenseValidationRequestInfo { DontShowForms = true, DisableTrials = true }; try { license = licenseManager.Validate(requestInfo); } catch (NoLicenseException ex) { validationException = ex; } } var licenseInfo = new LicenseInfo(license, licenseType) { ValidationException = validationException }; // if license expired we must specify that if (isExpired && license == null) { licenseInfo.IsExpired = true; licenseInfo.Serial = serial; } if (!licenseInfo.IsTrial || licenseInfos.All(x => !x.IsTrial)) { licenseInfos.Add(licenseInfo); } } isInitialized = true; UpdateExpirationDates(licenseInfos); var isValid = licenseInfos.Any(x => x.IsRegistered); return(isValid); }
/// <summary> /// Register with specified serial number /// </summary> /// <param name="serial">Serial number</param> public bool Register(string serial, string email) { if (string.IsNullOrWhiteSpace(serial)) { throw new PMInternalException(new NonLocalizableString("Serial is not defined.")); } var licenseType = GetTypeFromSerial(serial); if (!licenseType.HasValue) { throw new PMInternalException(new NonLocalizableString("Serial is not defined.")); } var licenseManager = ServiceLocator.Current.GetInstance <ILicenseManager>(licenseType.Value.ToString()); var licenseInfo = licenseInfos.FirstOrDefault(x => x.LicenseType == licenseType); var license = licenseInfo?.License; if (license != null) { licenseManager.ResetCacheForLicense(license); } if (licenseInfo != null) { licenseInfos.Remove(licenseInfo); } var requestInfo = new LicenseValidationRequestInfo { DontShowForms = true, SerialNumbers = new string[] { serial }, SaveExternalSerials = true, DisableTrials = true, DisableCache = true, ShouldGetNewSerialNumber = true }; requestInfo.AdditionalServerProperties.Add("email", email); var isExpired = false; try { license = licenseManager.Validate(requestInfo); if (license == null || license.IsTrial) { throw new LicenseInvalidSerialException("License not found or expired"); } if (license.IsActivation && !license.IsActivated) { throw new LicenseCouldNotActivateException(); } LogProvider.Log.Info(CustomModulesNames.PMCatcher, $"License has been registered: {license.SerialNumber.Substring(0, 4)}"); return(true); } catch (NoLicenseException ex) { // check for expiration first var exceptionData = ParseException(ex); if (exceptionData != null && exceptionData.ContainsKey("errorCode") && exceptionData["errorCode"].Equals("LCS_EXP")) { isExpired = true; LogProvider.Log.Error(CustomModulesNames.PMCatcher, "Registration: License expired", ex); throw new LicenseExpiredException(); } // check for activation errors var errorCodes = Utils.GetErrorCodes(ex); if (errorCodes.Any(x => x.Equals("E_CouldNotActivateAtServer") || x.Equals("E_CannotValidateAtServer"))) { LogProvider.Log.Error(CustomModulesNames.PMCatcher, "Registration: License wasn't activated", ex); throw new LicenseCouldNotActivateException(); } LogProvider.Log.Error(CustomModulesNames.PMCatcher, "Registration: License not found or expired", ex); throw new LicenseInvalidSerialException("License not found or expired", ex); } catch (LicenseCouldNotActivateException) { LogProvider.Log.Error(CustomModulesNames.PMCatcher, "Registration: License wasn't activated"); throw; } catch (Exception ex) { LogProvider.Log.Error(CustomModulesNames.PMCatcher, "Registration: License validation error", ex); throw new LicenseException("Unexpected license validation exception", ex); } finally { licenseInfo = new LicenseInfo(license, licenseType.Value); licenseInfos.Add(licenseInfo); // if license expired we must specify that if (isExpired && license == null) { licenseInfo.IsExpired = true; licenseInfo.Serial = serial; } UpdateExpirationDates(new[] { licenseInfo }); } }