public override void Execute(CancellationToken cancellationToken) { try { _logger.LogDebug("Validating license..."); base.Execute(cancellationToken); KeySecret = null; var appInfo = _remoteMetaData.GetAppInfo(); if (!appInfo.UseKeys) { _logger.LogDebug("Validating license is not required - application is not using license keys."); return; } var messageType = LicenseDialogMessageType.None; var cachedKey = GetCachedKey(); _logger.LogTrace("Cached key = " + cachedKey); bool didUseCachedKey = false; while (KeySecret == null) { bool isUsingCachedKey; string key = GetKey(messageType, cachedKey, out isUsingCachedKey, ref didUseCachedKey); try { _logger.LogTrace("Key = " + key); var cachedKeySecret = GetCachedKeySecret(key); _logger.LogTrace("Cached key secret = " + cachedKeySecret); _logger.LogDebug("Validating key..."); KeySecret = _remoteMetaData.GetKeySecret(key, cachedKeySecret); _logger.LogDebug("License has been validated!"); _logger.LogTrace("KeySecret = " + KeySecret); _logger.LogDebug("Saving key and key secret to cache."); SetCachedKey(key); SetCachedKeySecret(key, KeySecret); } catch (ApiResponseException apiResponseException) { _logger.LogWarning( "Key validation failed due to server or API error. Checking if error can be recognized and displayed to user...", apiResponseException); if (!TryToHandleApiErrors(apiResponseException.StatusCode, ref messageType, isUsingCachedKey)) { throw; } } catch (ApiConnectionException apiConnectionException) { _logger.LogWarning( "Key validation failed due to connection issues with API server. Setting license dialog message to ServiceUnavailable", apiConnectionException); messageType = LicenseDialogMessageType.ServiceUnavailable; } } } catch (Exception e) { _logger.LogError("Validating license has failed.", e); throw; } }
public override void Execute(CancellationToken cancellationToken) { base.Execute(cancellationToken); DebugLogger.Log("Validating license."); KeySecret = null; var appInfo = _remoteMetaData.GetAppInfo(); if (!appInfo.UseKeys) { DebugLogger.Log("Application is not using license keys."); return; } LicenseDialogMessageType messageType = LicenseDialogMessageType.None; var cachedKey = GetCachedKey(); bool triedCachedKey = false; while (KeySecret == null) { string key = string.Empty; if (!triedCachedKey && !string.IsNullOrEmpty(cachedKey)) { DebugLogger.Log("Using cached license key."); key = cachedKey; triedCachedKey = true; } else { DebugLogger.Log("Displaying license dialog."); var result = _licenseDialog.Display(messageType); DebugLogger.Log("Processing dialog result."); if (result.Type == LicenseDialogResultType.Confirmed) { DebugLogger.Log("Using license key typed in dialog."); key = result.Key; } else if (result.Type == LicenseDialogResultType.Aborted) { DebugLogger.Log("License dialog has been aborted. Cancelling operation."); throw new OperationCanceledException(); } } try { KeySecret = _remoteMetaData.GetKeySecret(key, GetCachedKeySecret(key)); DebugLogger.LogVariable(KeySecret, "KeySecret"); DebugLogger.Log("License key has been validated"); SetCachedKey(key); SetCachedKeySecret(key, KeySecret); } catch (ApiResponseException apiResponseException) { DebugLogger.LogException(apiResponseException); if (apiResponseException.StatusCode == 404) { KeySecret = null; messageType = LicenseDialogMessageType.InvalidLicense; } else if (apiResponseException.StatusCode == 410) { KeySecret = null; messageType = LicenseDialogMessageType.BlockedLicense; } else if (apiResponseException.StatusCode == 403) { KeySecret = null; messageType = LicenseDialogMessageType.ServiceUnavailable; } else { throw; } } catch (WebException webException) { DebugLogger.LogException(webException); KeySecret = null; messageType = LicenseDialogMessageType.ServiceUnavailable; if (webException.Status == WebExceptionStatus.ProtocolError) { var response = (HttpWebResponse)webException.Response; if ((int)response.StatusCode == 404) { messageType = LicenseDialogMessageType.InvalidLicense; } else if ((int)response.StatusCode == 410) { messageType = LicenseDialogMessageType.BlockedLicense; } else if ((int)response.StatusCode == 403) { messageType = LicenseDialogMessageType.ServiceUnavailable; } } } } }