private Token GetTokenWithPrompt(OAuthLowLevel oAuth, OAuthOptions options)
        {
            Token token = null;

            using (DefaultWebBrowserOverride webBrowserOverride = new DefaultWebBrowserOverride(new AuthorisationResultUrlChecker()))
            {
                _cancellationTokenSource = new CancellationTokenSource();

                Auth0ClientBase client = CreateAuth0Client();

                var startResult = Task.Run(() => oAuth.BeginAuthorisationAsync(client, Audience, options)).Result;

                string endUrl = Task.Run(() => webBrowserOverride.NavigateToStartUrlAsync(new Uri(startResult.StartUrl), _cancellationTokenSource.Token)).Result;

                var endResult = Task.Run(() => oAuth.EndAuthorisationAsync(client, startResult, endUrl, ClientID, Scope, Audience, StoragePartition)).Result;

                token = endResult.AccessToken;
            }

            return(token);
        }
        private Token GetTokenSilently(OAuthLowLevel oAuth)
        {
            Token token = null;

            token = oAuth.RetrieveExistingAccessToken(ClientID, Scope, Audience, StoragePartition);

            if (token == null)
            {
                Token refreshToken = oAuth.RetrieveExistingRefreshToken(ClientID, Scope, Audience, StoragePartition);
                if (refreshToken != null)
                {
                    Auth0ClientBase client = CreateAuth0Client();

                    AuthorisationResult result = Task.Run(() => oAuth.RefreshAccessTokenAsync(client, refreshToken, ClientID, Scope, Audience, StoragePartition)).Result;

                    token = result.AccessToken;
                }
            }

            return(token);
        }
        protected string LogonInner(bool resetDuration)
        {
            string ret   = string.Empty;
            Token  token = null;

            bool retry = false;

            OAuthLowLevel oAuth = new OAuthLowLevel(LoggingOverride, _accessTokenStorageOverride, _refreshTokenStorageOverride);

            do
            {
                retry = false;

                // Start a stopwatch to check if time elapsed has exceeded configured timeout
                System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
                stopwatch.Start();

                try
                {
                    try
                    {
                        if (resetDuration == false)
                        {
                            token = GetTokenSilently(oAuth);
                        }

                        if (token == null)
                        {
                            OAuthOptions options = new OAuthOptions();
                            options.ForceUserInteraction = resetDuration;
                            token = GetTokenWithPrompt(oAuth, options);
                        }
                    }
                    catch (AggregateException agg)
                    {
                        HandleAggregateException(agg);
                    }
                }
                catch (System.Threading.Tasks.TaskCanceledException)
                {
                    throw new AuthenticationCancelException();
                }
                catch (Exception ex)
                {
                    if (stopwatch.Elapsed > _logonTimeout)
                    {
                        retry = true;
                    }
                    else
                    {
                        throw new AuthenticationException(ex.Message);
                    }
                }
            } while (retry);

            // Return the access token
            if (token != null)
            {
                ret = token.RawToken.FromSecureString();
            }

            return(ret);
        }