예제 #1
0
        private async Task <MEXJwtToken> RetrieveNewRefreshToken(ICacheEntry entry)
        {
            using var httpClient = this.HttpClientFactory.CreateClient(MEXAuthenticationService.HttpClientName);

            var authDetails = this.GetAuthenticationDetails();
            var response    = await httpClient.PostAsJsonAsync("RequestRefreshToken", authDetails);

            response.EnsureSuccessStatusCode();

            var result = await response.Content.ReadFromJsonAsync <ODataFunctionImportQueryableData>();

            if (!result.FieldValue.Contains("SUCCESS", StringComparison.InvariantCultureIgnoreCase))
            {
                throw new MEXAuthenticationException("Failed to retrieve new Refresh Token.");
            }

            var accessTokenString = response.Headers.First(x => x.Key == "Set-AccessToken").Value.First();
            var accessToken       = new MEXJwtToken(accessTokenString);

            this.MemoryCache.Set(MEXAuthenticationService.AccessTokenCacheKey, accessToken, accessToken.ExpirationDateTime);

            var refreshTokenString = response.Headers.First(x => x.Key == "Set-RefreshToken").Value.First();
            var refreshToken       = new MEXJwtToken(refreshTokenString);

            entry.AbsoluteExpiration = refreshToken.ExpirationDateTime;

            return(refreshToken);
        }
예제 #2
0
        private async Task <MEXJwtToken> RetrieveNewAccessToken(ICacheEntry entry)
        {
            var refreshToken = await this.GetRefreshToken();

            // TODO: Wanna clean this up.
            // Check if a new access token was stored when we requested the refresh token
            if (this.MemoryCache.TryGetValue(MEXAuthenticationService.AccessTokenCacheKey, out MEXJwtToken existingAccessToken))
            {
                return(existingAccessToken);
            }

            using var httpClient = this.HttpClientFactory.CreateClient(MEXAuthenticationService.HttpClientName);
            var httpRequest = new HttpRequestMessage(HttpMethod.Post, "GenerateAccessToken?useCookies=false");

            httpRequest.Headers.Add("RefreshToken", refreshToken.ToString());

            var response = await httpClient.SendAsync(httpRequest);

            var accessTokenString = response.Headers.First(x => x.Key == "Set-AccessToken").Value.First();
            var accessToken       = new MEXJwtToken(accessTokenString);

            entry.AbsoluteExpiration = accessToken.ExpirationDateTime;

            return(accessToken);
        }