public ServiceProduct CreateTestProduct(string licenseKey) { DeleteTestServiceProduct(); ServiceProduct prod = new ServiceProduct(); prod.LicenseId = int.MaxValue; prod.LicenseName = "Test Product License"; prod.LicenseSets = new List <ServiceLicenseSet>(); ServiceLicenseSet ls = new ServiceLicenseSet(); ls.LicenseSetId = int.MaxValue; ls.LicenseId = int.MaxValue; ls.Keys = new List <ServiceLicenseKey>(); ls.LicenseSetName = "Test Product License Set"; ls.LicenseType = LicenseKeyTypeFlag.MultiUser; ls.MaxUsers = 2; ServiceLicenseKey key = new ServiceLicenseKey(); key.CreatedOn = DateTime.Now; key.Key = _hashingProvider.ComputeHash(licenseKey, "SHA256"); key.LicenseSetId = int.MaxValue; key.ActivationCount = 0; ls.Keys.Add(key); prod.LicenseSets.Add(ls); _serviceProductsRepository.SaveServiceProduct(prod); return(_serviceProductsRepository.GetProduct(int.MaxValue)); }
public ServiceLicenseKey SaveServiceLicenseKey(ServiceLicenseKey serviceLicenseKey) { if (GetServiceLicenseKeyByKey(serviceLicenseKey.Key) != null) { UpdateServiceLicenseKey(serviceLicenseKey); } else { InsertServiceLicenseKey(serviceLicenseKey); } return(GetServiceLicenseKeyByKey(serviceLicenseKey.Key)); }
private void InsertServiceLicenseKey(ServiceLicenseKey serviceLicenseKey) { using (ScutexServiceEntities db1 = new ScutexServiceEntities()) { LicenseKey licKey = new LicenseKey(); licKey.Key = serviceLicenseKey.Key; licKey.ActivationCount = serviceLicenseKey.ActivationCount; licKey.Deactivated = serviceLicenseKey.Deactivated; licKey.DeactivatedOn = serviceLicenseKey.DeactivatedOn; licKey.DeactivatedReason = serviceLicenseKey.DeactivatedReason; licKey.LicenseSetId = serviceLicenseKey.LicenseSetId; licKey.CreatedOn = serviceLicenseKey.CreatedOn; db1.AddToLicenseKeys(licKey); db1.SaveChanges(); } }
public void AddLicenseKeysForLicenseSet(int licenseSetId, List <string> licenseKeys) { foreach (string k in licenseKeys) { if (DoesKeyExistForLicenseSet(k, licenseSetId) == false) { ServiceLicenseKey key = new ServiceLicenseKey(); key.ActivationCount = 0; key.CreatedOn = DateTime.Now; key.Deactivated = false; key.Key = k; key.LicenseSetId = licenseSetId; _serviceProductsRepository.SaveServiceLicenseKey(key); } } }
private void UpdateServiceLicenseKey(ServiceLicenseKey serviceLicenseKey) { using (ScutexServiceEntities db1 = new ScutexServiceEntities()) { var licKey = (from lk in db1.LicenseKeys where lk.Key == serviceLicenseKey.Key select lk).First(); licKey.Key = serviceLicenseKey.Key; licKey.CreatedOn = serviceLicenseKey.CreatedOn; licKey.ActivationCount = serviceLicenseKey.ActivationCount; licKey.Deactivated = serviceLicenseKey.Deactivated; licKey.DeactivatedOn = serviceLicenseKey.DeactivatedOn; licKey.DeactivatedReason = serviceLicenseKey.DeactivatedReason; licKey.LicenseSetId = serviceLicenseKey.LicenseSetId; db1.SaveChanges(); } }
/// <summary> /// Checks the database to see if the key has already been used, and if it's a multi-use capable key /// the number of valid activations will be check to ensure it doesn't exceed the maximum activation /// count. If the key is hardware based it will check the existing key's fingerprint against the one /// saved to see if it matches. /// </summary> /// <param name="licenseKey"></param> /// <param name="licenseSetId"></param> /// <param name="hardwareFingerprint"></param> /// <returns></returns> public bool IsKeyAvialable(string licenseKey, int licenseSetId, string hardwareFingerprint) { ServiceLicenseSet ls = _serviceProductsRepository.GetServiceLicenseSetById(licenseSetId); ServiceLicenseKey lk = _serviceProductsRepository.GetServiceLicenseKeyByKeyLicenseSet(licenseKey, licenseSetId); LicenseActivation la = _clientRepository.GetLicenseActivationByKeyAndSetId(licenseKey, licenseSetId); if (la == null) { return(true); } Debug.WriteLine(ls.LicenseType.ToString()); // Enterprise and Unlimited keys have no activation restrictions if (ls.LicenseType.IsSet(LicenseKeyTypeFlag.Unlimited) || ls.LicenseType.IsSet(LicenseKeyTypeFlag.Enterprise)) { return(true); } // If the key is multi-user, ensure that they are under the MaxUsers count if (ls.LicenseType.IsSet(LicenseKeyTypeFlag.MultiUser)) { if (ls.MaxUsers.HasValue && ((lk.ActivationCount + 1) <= ls.MaxUsers.Value)) { return(true); } } // Hardware keys can activate as many times as they want, provided the hardware fingerprints match. if (ls.LicenseType.IsSet(LicenseKeyTypeFlag.HardwareLock)) { if (la != null) { if (la.HardwareHash == hardwareFingerprint) { return(true); } } } return(false); }
/// <summary> /// Attempts to activate the license key /// </summary> /// <param name="licenseKey"></param> /// <param name="originalToken"></param> /// <param name="licenseBase"></param> /// <param name="hardwareFingerprint"></param> /// <returns></returns> public Guid?ActivateLicenseKey(string licenseKey, Guid?originalToken, ServiceLicense licenseBase, string hardwareFingerprint) { KeyData keyData = _licenseKeyService.GetLicenseKeyData(licenseKey, licenseBase, true); SecureString hashedLicenseKey = SecureStringHelper.StringToSecureString(_hashingProvider.ComputeHash(licenseKey, Resources.KeyHashAlgo)); hashedLicenseKey.MakeReadOnly(); int licenseSetId = keyData.LicenseSetId;// TODO: Only works for SSLK LicenseSet ls = _clientRepository.GetLicenseSetById(licenseSetId); LicenseActivation la = _clientRepository.GetLicenseActivationByKeyAndSetId(SecureStringHelper.SecureStringToString(hashedLicenseKey), licenseSetId); if (AuthorizeLicenseForActivation(licenseKey, licenseBase, hardwareFingerprint)) // TODO: Possible double call with two log entries here, as this is called in the parent as well. -SJ { ServiceLicenseKey lk = _serviceProductsRepository.GetServiceLicenseKeyByKeyLicenseSet(SecureStringHelper.SecureStringToString(hashedLicenseKey), licenseSetId); la = new LicenseActivation(); la.LicenseKeyId = lk.LicenseKeyId; la.ActivatedOn = DateTime.Now; la.ActivationToken = Guid.NewGuid(); la.OriginalToken = originalToken; la.ActivationStatus = ActivationStatus.Normal; la.ActivationStatusUpdatedOn = DateTime.Now; if (!String.IsNullOrEmpty(hardwareFingerprint)) { la.HardwareHash = hardwareFingerprint; } _clientRepository.InsertLicenseActivation(la); lk.ActivationCount++; _serviceProductsRepository.SaveServiceLicenseKey(lk); return(la.ActivationToken); } return(null); }