예제 #1
0
        private void RefreshToken()
        {
            var tokenRequest = new Auth0TokenRequest
            {
                GrantType    = "client_credentials",
                ClientId     = "==== This really should not be here; can get it from config ====",
                ClientSecret = "==== This really should not be here; can get it from config ====",
                Audience     = "https://maesure.auth0.com/api/v2/"
            };

            try
            {
                var tokenReply = _restClient.Post <Auth0TokenReply>(_config.TokenUrl(), tokenRequest).Result;

                if (!tokenReply.IsSuccess)
                {
                    _log.LogError("Failed to get Auth0 JWT. Details: " + tokenReply.Error);
                    StartTimer(RetryIntervalMsec);
                    return;
                }

                _token = tokenReply.Result.AccessToken;
                _log.LogInformation("Reloaded Auth0 token.");
                StartTimer(((double)tokenReply.Result.ExpiresInSec * 1000) / 2);
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "Exception while getting Auth0 JWT token:");
                StartTimer(RetryIntervalMsec);
            }
        }
예제 #2
0
        public async Task <string> GetManagementApiTokenAsync()
        {
            if (_externalAuthOptions == null)
            {
                return(null);
            }

            if (!string.IsNullOrEmpty(_accessToken) && _tokenLastRetrieved + _tokenExpiration > DateTimeOffset.UtcNow)
            {
                return(_accessToken);
            }

            var obj = new Auth0TokenRequest {
                grant_type    = "client_credentials",
                client_id     = _externalAuthOptions["Auth0:ManagementClientId"],
                client_secret = _externalAuthOptions["Auth0:ManagementClientSecret"],
                audience      = $"https://{_externalAuthOptions["Auth0:Domain"]}/api/v2/"
            };
            var content  = new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json");
            var response = await _client.PostAsync($"https://{_externalAuthOptions["Auth0:Domain"]}/oauth/token", content);

            response.EnsureSuccessStatusCode();
            var accessTokenJson = await response.Content.ReadAsStringAsync();

            var accessTokenResponse = JsonConvert.DeserializeObject <Auth0TokenResponse>(accessTokenJson);

            _accessToken        = accessTokenResponse.access_token;
            _tokenExpiration    = TimeSpan.FromSeconds(accessTokenResponse.expires_in);
            _tokenLastRetrieved = DateTimeOffset.UtcNow;
            return(_accessToken);
        }