Пример #1
0
        private async Task <string?> RenewTokenAsync(ILogger log)
        {
            if (string.IsNullOrEmpty(packageId) || string.IsNullOrEmpty(secret))
            {
                return(null);
            }

            Dictionary <string, string> content = new Dictionary <string, string>()
            {
                { "grant_type", "client_credentials" },
                { "client_id", packageId },
                { "client_secret", secret },
                { "scope", tokenRequestScope }
            };
            HttpResponseMessage response = await httpClient.PostAsync(tokenRequestUri, new FormUrlEncodedContent(content));

            if (!response.IsSuccessStatusCode)
            {
                log.LogError("WNS service returned error status while renewing tokens. Status code: {statusCode}", response.StatusCode);
                return(null);
            }
            try
            {
                PushAccessToken token = await JsonSerializer.DeserializeAsync <PushAccessToken>(await response.Content.ReadAsStreamAsync());

                token.Platform = PushPlatform.Wns;
                DataAccessResult updateResult = await dataService.SetPushAccessTokenAsync(token);

                if (!updateResult.Success)
                {
                    log.LogWarning("Unable to write new WNS token to database. Status code: {statusCode}", updateResult.StatusCode);
                }
                return(token.Token);
            }
            catch (JsonException ex)
            {
                log.LogError(ex, $"WNS service returned unexpected response content.");
                return(null);
            }
        }
Пример #2
0
 public Task <DataAccessResult> SetPushAccessTokenAsync(PushAccessToken token) => SetResourceAsync <PushAccessToken>(token);