private string GetKeyFromDialog(LicenseDialogMessageType messageType)
        {
            _logger.LogDebug("Displaying license dialog...");

            var result = _licenseDialog.Display(messageType);

            _logger.LogDebug("License dialog has returned result.");

            _logger.LogTrace("result.Key = " + result.Key);
            _logger.LogTrace(string.Format("result.Type = {0}", result.Type));

            switch (result.Type)
            {
            case LicenseDialogResultType.Confirmed:
                _logger.LogDebug("Using key typed in license dialog.");
                return(result.Key);

            case LicenseDialogResultType.Aborted:
                _logger.LogDebug("License dialog has been aborted. Cancelling operation.");
                throw new OperationCanceledException();

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Beispiel #2
0
        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;
                        }
                    }
                }
            }
        }