Example #1
0
        private async Task OnActivityStateChanged(ActivityEventArgs eventArgs)
        {
            _retryCancellationTokenSource.Cancel();
            _retryCancellationTokenSource = new CancellationTokenSource();

            var logTimeModel = new LogTimeModel()
            {
                IsActive  = eventArgs.IsActive,
                Timestamp = eventArgs.TimeStamp,
            };

            var httpContent = new StringContent(
                JsonSerializer.Serialize(logTimeModel),
                Encoding.UTF8,
                "application/json");

            await _retryPolicy.ExecuteAsync(
                (ctx, ct) => _httpClient.PostAsync(FunctionMap.Get(Function.LogTime), httpContent, ct),
                _retryContext,
                _retryCancellationTokenSource.Token);
        }
        public async Task TryRefreshToken(CancellationToken?cancellationToken = null)
        {
            using var request = new HttpRequestMessage(HttpMethod.Get, FunctionMap.Get(Function.RefreshJwtToken));
            request.SetBrowserRequestCredentials(BrowserRequestCredentials.Include);
            var response = await _httpClient.SendAsync(request, cancellationToken ?? CancellationToken.None);

            string jwtToken = null !;

            if (response.IsSuccessStatusCode)
            {
                var responseString = await response.Content.ReadAsStringAsync();

                if (!responseString.IsNullOrEmpty())
                {
                    var refreshTokenResult = JsonSerializer.Deserialize <RefreshTokenResult>(responseString);

                    jwtToken = refreshTokenResult.Token;
                }
            }

            _authService.JwtToken = jwtToken;
        }