diff --git a/README.md b/README.md index ff7962f..3b4c684 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ This library hopes to solve that problem, featuring: :white_check_mark: `IHttpClientBuilder` extensions, providing handlers to automatically append access tokens to outgoing requests. - This library is compatible with all .NET Standard 2.0 runtimes (.NET 8+ recommended) and is suitable for use in ASP.NET Core and standalone .NET Generic Host applications. + This library is compatible with .NET 8+ as well as .NET Framework 4.8 and is suitable for use in ASP.NET Core and standalone .NET Generic Host applications. ## Install diff --git a/build/Build.cs b/build/Build.cs index cc4ac29..2d0a350 100644 --- a/build/Build.cs +++ b/build/Build.cs @@ -10,7 +10,6 @@ using Nuke.Common.Tools.DotNet; using Nuke.Common.Utilities.Collections; using static Nuke.Common.EnvironmentInfo; -using static Nuke.Common.IO.FileSystemTasks; using static Nuke.Common.IO.PathConstruction; using static Nuke.Common.Tools.DotNet.DotNetTasks; diff --git a/build/_build.csproj b/build/_build.csproj index 7694661..9cc739d 100644 --- a/build/_build.csproj +++ b/build/_build.csproj @@ -11,7 +11,7 @@ - + diff --git a/samples/Sample.AspNetCore/Sample.AspNetCore.csproj b/samples/Sample.AspNetCore/Sample.AspNetCore.csproj index 6918c82..0863285 100644 --- a/samples/Sample.AspNetCore/Sample.AspNetCore.csproj +++ b/samples/Sample.AspNetCore/Sample.AspNetCore.csproj @@ -8,7 +8,7 @@ - + diff --git a/samples/Sample.ConsoleApp/Sample.ConsoleApp.csproj b/samples/Sample.ConsoleApp/Sample.ConsoleApp.csproj index c20668a..f4260c4 100644 --- a/samples/Sample.ConsoleApp/Sample.ConsoleApp.csproj +++ b/samples/Sample.ConsoleApp/Sample.ConsoleApp.csproj @@ -9,9 +9,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/Auth0Net.DependencyInjection/Auth0Extensions.cs b/src/Auth0Net.DependencyInjection/Auth0Extensions.cs index 688f5b6..8c4ed1c 100644 --- a/src/Auth0Net.DependencyInjection/Auth0Extensions.cs +++ b/src/Auth0Net.DependencyInjection/Auth0Extensions.cs @@ -91,7 +91,7 @@ private static IHttpClientBuilder AddAuth0AuthenticationClientInternal(this ISer services.AddSingleton(); return services.AddHttpClient() -#if !NETSTANDARD2_0 +#if !NETFRAMEWORK .ConfigurePrimaryHttpMessageHandler(() => new SocketsHttpHandler() { @@ -116,7 +116,7 @@ public static IHttpClientBuilder AddAuth0ManagementClient(this IServiceCollectio services.AddSingleton(); return services.AddHttpClient() -#if !NETSTANDARD2_0 +#if !NETFRAMEWORK .ConfigurePrimaryHttpMessageHandler(() => new SocketsHttpHandler() { diff --git a/src/Auth0Net.DependencyInjection/Auth0Net.DependencyInjection.csproj b/src/Auth0Net.DependencyInjection/Auth0Net.DependencyInjection.csproj index c10468d..7a4f2f0 100644 --- a/src/Auth0Net.DependencyInjection/Auth0Net.DependencyInjection.csproj +++ b/src/Auth0Net.DependencyInjection/Auth0Net.DependencyInjection.csproj @@ -1,16 +1,16 @@  - netstandard2.0;net8.0;net9.0 + net48;net8.0;net9.0 true enable enable - 4.0.0 + 5.0.0 Hawxy Dependency Injection, HttpClientFactory & ASP.NET Core extensions for Auth0.NET latest true - Hawxy (JT) 2020-2024 + Hawxy (JT) 2020-2025 icon.png MIT https://github.com/Hawxy/Auth0Net.DependencyInjection @@ -21,30 +21,31 @@ - - + + - - + + - - + + + + - - + <_Parameter1>$(MSBuildProjectName).Tests diff --git a/src/Auth0Net.DependencyInjection/HttpClient/Auth0ResilienceExtensions.cs b/src/Auth0Net.DependencyInjection/HttpClient/Auth0ResilienceExtensions.cs new file mode 100644 index 0000000..18abec4 --- /dev/null +++ b/src/Auth0Net.DependencyInjection/HttpClient/Auth0ResilienceExtensions.cs @@ -0,0 +1,52 @@ +#if NET8_0_OR_GREATER +using System.Diagnostics.CodeAnalysis; +using System.Net; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Http.Resilience; +using Polly; + +namespace Auth0Net.DependencyInjection.HttpClient; + +/// +/// Extensions used to enhance Auth0 client resilience. +/// +public static class Auth0ResilienceExtensions +{ + /// + /// Adds enhanced rate limiting support to the Auth0 Client. This API is experimental. + /// + /// + /// + [Experimental("Auth0DIExperimental")] + public static IHttpResiliencePipelineBuilder AddAuth0RateLimitResilience(this IHttpClientBuilder builder) + { + return builder.AddResilienceHandler("RateLimitRetry", + static builder => + { + // See: https://www.pollydocs.org/strategies/retry.html + builder.AddRetry(new HttpRetryStrategyOptions + { + // Disable the default handling of Retry-After header + ShouldRetryAfterHeader = false, + DelayGenerator = static args => + { + if(args.Outcome.Result?.StatusCode is (HttpStatusCode)429 + && args.Outcome.Result.Headers.TryGetValues("x-ratelimit-reset", out var headers) + && long.TryParse(headers.First(), out var ticks)) + { + var retryAt = DateTimeOffset.FromUnixTimeSeconds(ticks); + var timeSpan = retryAt - DateTimeOffset.UtcNow; + return new ValueTask(timeSpan); + } + + return new ValueTask((TimeSpan?)null); + }, + + MaxRetryAttempts = 10, + Delay = TimeSpan.FromSeconds(2) + }); + }); + } +} + +#endif \ No newline at end of file diff --git a/src/Auth0Net.DependencyInjection/HttpClient/Auth0TokenHandler.cs b/src/Auth0Net.DependencyInjection/HttpClient/Auth0TokenHandler.cs index d6fbc47..d5b4869 100644 --- a/src/Auth0Net.DependencyInjection/HttpClient/Auth0TokenHandler.cs +++ b/src/Auth0Net.DependencyInjection/HttpClient/Auth0TokenHandler.cs @@ -1,4 +1,5 @@ -using System.Net.Http.Headers; +using System.Net.Http; +using System.Net.Http.Headers; using Auth0Net.DependencyInjection.Cache; using Microsoft.Extensions.Options; diff --git a/src/Auth0Net.DependencyInjection/HttpClient/Auth0TokenHandlerConfig.cs b/src/Auth0Net.DependencyInjection/HttpClient/Auth0TokenHandlerConfig.cs index 2b6d2d7..493874c 100644 --- a/src/Auth0Net.DependencyInjection/HttpClient/Auth0TokenHandlerConfig.cs +++ b/src/Auth0Net.DependencyInjection/HttpClient/Auth0TokenHandlerConfig.cs @@ -1,4 +1,6 @@ -namespace Auth0Net.DependencyInjection.HttpClient; +using System.Net.Http; + +namespace Auth0Net.DependencyInjection.HttpClient; /// /// Configuration used by the underlying . diff --git a/tests/Auth0Net.DependencyInjection.Tests/Auth0Net.DependencyInjection.Tests.csproj b/tests/Auth0Net.DependencyInjection.Tests/Auth0Net.DependencyInjection.Tests.csproj index c62ec90..e46aa2f 100644 --- a/tests/Auth0Net.DependencyInjection.Tests/Auth0Net.DependencyInjection.Tests.csproj +++ b/tests/Auth0Net.DependencyInjection.Tests/Auth0Net.DependencyInjection.Tests.csproj @@ -2,19 +2,19 @@ false - net48;net8.0;net9.0 + net8.0;net9.0 latest - - - - + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all