public string GetAccessToken() { return(_cache.GetOrCreate(CacheKey, cacheEntry => { _logger.LogInformation("Cached AccessToken Expired, Generating new Token"); var token = _inner.GetAccessToken(); cacheEntry.SetAbsoluteExpiration(DateTimeOffset.UtcNow.AddMinutes(50)); _logger.LogInformation("New SQL Access Token Generated, Caching for an hour"); return token; })); }
public override InterceptionResult ConnectionOpening( DbConnection connection, ConnectionEventData eventData, InterceptionResult result) { var sqlConnection = (SqlConnection)connection; if (_connectionNeedsAccessToken) { _logger.LogInformation("Getting AccessToken"); var token = _tokenProvider.GetAccessToken(); sqlConnection.AccessToken = token; } else { _logger.LogWarning($"Skipping GetAccessToken because ConnectionNeedsAccessToken is {_connectionNeedsAccessToken}"); } return(base.ConnectionOpening(connection, eventData, result)); }