protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request,
                                                                      CancellationToken cancellationToken)
        {
            if (request.AuthHeadersInvalid())
            {
                SessionExpired?.Invoke(this, EventArgs.Empty);
                return(FailResponse.UnauthorizedResponse());
            }

            Task <RefreshTokenStatus> refreshTask = _refreshTask;

            if (!refreshTask.IsCompleted)
            {
                RefreshTokenStatus refreshSucceeded = await refreshTask;
                return(await ResendAsync(request, cancellationToken, refreshSucceeded));
            }

            HttpResponseMessage response = await base.SendAsync(request, cancellationToken);

            if (response.StatusCode == HttpStatusCode.Unauthorized && !_userStorage.User().Empty())
            {
                try
                {
                    RefreshTokenStatus refreshSucceeded = await Refresh(refreshTask, cancellationToken);

                    return(await ResendAsync(request, cancellationToken, refreshSucceeded));
                }
                finally
                {
                    response.Dispose();
                }
            }

            return(response);
        }
        private async Task <HttpResponseMessage> ResendAsync(HttpRequestMessage request,
                                                             CancellationToken cancellationToken,
                                                             RefreshTokenStatus refreshTokenStatus)
        {
            switch (refreshTokenStatus)
            {
            case RefreshTokenStatus.Success:
                PrepareRequest(request);
                return(await base.SendAsync(request, cancellationToken));

            case RefreshTokenStatus.Unauthorized:
                SessionExpired?.Invoke(this, EventArgs.Empty);
                return(FailResponse.UnauthorizedResponse());

            default:
                return(FailResponse.UnauthorizedResponse());
            }
        }