Skip to content

Commit dc0c804

Browse files
authored
v5.1.0 (#31)
Co-authored-by: JT <Hawxy@users.noreply.github.com>
1 parent 5c9532d commit dc0c804

File tree

8 files changed

+47
-33
lines changed

8 files changed

+47
-33
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,22 @@ services.AddHttpClient<MyHttpService>(x=> x.BaseAddress = new Uri("https://MySer
140140
.AddAccessToken(config => config.AudienceResolver = request => request.RequestUri.GetLeftPart(UriPartial.Authority));
141141
```
142142

143+
### Enhanced Resilience
144+
145+
The default rate-limit behaviour in Auth0.NET is suboptimal, as it uses random backoff rather than reading the rate limit headers returned by Auth0.
146+
This package includes an additional `.AddAuth0RateLimitResilience()` extension that adds improved rate limit handling to the Auth0 clients.
147+
If you're running into rate limit failures, I highly recommend adding this functionality:
148+
149+
```csharp
150+
services.AddAuth0ManagementClient()
151+
.AddManagementAccessToken()
152+
.AddAuth0RateLimitResilience();
153+
```
154+
155+
When a retry occurs, you should see a warning log similar to:
156+
157+
`Resilience event occurred. EventName: '"OnRetry"', Source: '"IManagementConnection-RateLimitRetry"/""/"Retry"', Operation Key: 'null', Result: '429'`
158+
143159
### Client Lifetimes
144160

145161
Both the authentication and authorization clients are registered as singletons and are suitable for injection into any other lifetime.

samples/Sample.AspNetCore/Sample.AspNetCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Grpc.AspNetCore" Version="2.67.0" />
11+
<PackageReference Include="Grpc.AspNetCore" Version="2.71.0" />
1212
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0" />
1313
</ItemGroup>
1414

samples/Sample.ConsoleApp/Sample.ConsoleApp.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Google.Protobuf" Version="3.29.2" />
13-
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.67.0" />
14-
<PackageReference Include="Grpc.Tools" Version="2.68.1">
12+
<PackageReference Include="Google.Protobuf" Version="3.30.2" />
13+
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.71.0" />
14+
<PackageReference Include="Grpc.Tools" Version="2.72.0">
1515
<PrivateAssets>all</PrivateAssets>
1616
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1717
</PackageReference>

src/Auth0Net.DependencyInjection/Auth0Extensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ private static IHttpClientBuilder AddAuth0AuthenticationClientInternal(this ISer
9292
services.AddSingleton<IAuthenticationApiClient, InjectableAuthenticationApiClient>();
9393
return services.AddHttpClient<IAuthenticationConnection, HttpClientAuthenticationConnection>()
9494
#if !NETFRAMEWORK
95+
// TODO drop this code with the release of .NET 10
9596
.ConfigurePrimaryHttpMessageHandler(() =>
9697
new SocketsHttpHandler()
9798
{

src/Auth0Net.DependencyInjection/Auth0Net.DependencyInjection.csproj

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<GenerateDocumentationFile>true</GenerateDocumentationFile>
66
<Nullable>enable</Nullable>
77
<ImplicitUsings>enable</ImplicitUsings>
8-
<Version>5.0.0</Version>
8+
<Version>5.1.0</Version>
99
<Authors>Hawxy</Authors>
1010
<Description>Dependency Injection, HttpClientFactory &amp; ASP.NET Core extensions for Auth0.NET</Description>
1111
<LangVersion>latest</LangVersion>
@@ -21,29 +21,29 @@
2121
</PropertyGroup>
2222

2323
<ItemGroup>
24-
<PackageReference Include="Auth0.AuthenticationApi" Version="7.32.0" />
25-
<PackageReference Include="Auth0.ManagementApi" Version="7.32.0" />
24+
<PackageReference Include="Auth0.AuthenticationApi" Version="7.36.0" />
25+
<PackageReference Include="Auth0.ManagementApi" Version="7.36.0" />
2626
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.2.25" PrivateAssets="All" />
27-
<PackageReference Include="ZiggyCreatures.FusionCache" Version="2.0.0" />
28-
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.1" />
27+
<PackageReference Include="ZiggyCreatures.FusionCache" Version="2.2.0" />
28+
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.4" />
2929
</ItemGroup>
3030

3131
<ItemGroup Condition=" '$(TargetFramework)' == 'net48' ">
32-
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
33-
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
34-
<PackageReference Include="Polyfill" Version="7.4.1" PrivateAssets="all" />
32+
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.1" />
33+
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.1" />
34+
<PackageReference Include="Polyfill" Version="7.31.0" PrivateAssets="all" />
3535
</ItemGroup>
3636

3737
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
38-
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
39-
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.0.0" />
38+
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
39+
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.10.0" />
4040
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
4141
</ItemGroup>
4242

4343
<ItemGroup Condition=" '$(TargetFramework)' == 'net9.0' ">
44-
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.0" />
45-
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="9.0.0" />
46-
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.0" />
44+
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.4" />
45+
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="9.4.0" />
46+
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.4" />
4747
</ItemGroup>
4848

4949
<ItemGroup>

src/Auth0Net.DependencyInjection/HttpClient/Auth0ResilienceExtensions.cs renamed to src/Auth0Net.DependencyInjection/Auth0ResilienceExtensions.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
#if NET8_0_OR_GREATER
2-
using System.Diagnostics.CodeAnalysis;
32
using System.Net;
43
using Microsoft.Extensions.DependencyInjection;
54
using Microsoft.Extensions.Http.Resilience;
65
using Polly;
76

8-
namespace Auth0Net.DependencyInjection.HttpClient;
7+
namespace Auth0Net.DependencyInjection;
98

109
/// <summary>
1110
/// Extensions used to enhance Auth0 client resilience.
1211
/// </summary>
1312
public static class Auth0ResilienceExtensions
1413
{
1514
/// <summary>
16-
/// Adds enhanced rate limiting support to the Auth0 Client. This API is experimental.
15+
/// Adds enhanced rate limiting support to the Auth0 Client.
1716
/// </summary>
18-
/// <param name="builder"></param>
17+
/// <param name="builder">The underlying <see cref="IHttpClientBuilder"/></param>
18+
/// <param name="maxRetryAttempts">The max number of retry attempts to Auth0. Defaults to 10.</param>
1919
/// <returns></returns>
20-
[Experimental("Auth0DIExperimental")]
21-
public static IHttpResiliencePipelineBuilder AddAuth0RateLimitResilience(this IHttpClientBuilder builder)
20+
public static IHttpResiliencePipelineBuilder AddAuth0RateLimitResilience(this IHttpClientBuilder builder, int maxRetryAttempts = 10)
2221
{
2322
return builder.AddResilienceHandler("RateLimitRetry",
24-
static builder =>
23+
pipelineBuilder =>
2524
{
26-
// See: https://www.pollydocs.org/strategies/retry.html
27-
builder.AddRetry(new HttpRetryStrategyOptions
25+
pipelineBuilder.AddRetry(new HttpRetryStrategyOptions
2826
{
2927
// Disable the default handling of Retry-After header
3028
ShouldRetryAfterHeader = false,
@@ -41,8 +39,7 @@ public static IHttpResiliencePipelineBuilder AddAuth0RateLimitResilience(this IH
4139

4240
return new ValueTask<TimeSpan?>((TimeSpan?)null);
4341
},
44-
45-
MaxRetryAttempts = 10,
42+
MaxRetryAttempts = maxRetryAttempts,
4643
Delay = TimeSpan.FromSeconds(2)
4744
});
4845
});

tests/Auth0Net.DependencyInjection.Tests/Auth0Net.DependencyInjection.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
<ItemGroup>
1010
<PackageReference Include="FakeItEasy" Version="8.3.0" />
1111
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
12-
<PackageReference Include="xunit.v3" Version="1.0.1" />
13-
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1">
12+
<PackageReference Include="xunit.v3" Version="2.0.2" />
13+
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.0">
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1515
<PrivateAssets>all</PrivateAssets>
1616
</PackageReference>

tests/Auth0Net.DependencyInjection.Tests/CacheTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public async Task Cache_WorksAsExpected()
4040
var cache = new Auth0TokenCache(authClient, new FusionCacheTestProvider(), new NullLogger<Auth0TokenCache>(), config);
4141

4242
var key = "api://my-audience";
43-
var resFirst = await cache.GetTokenAsync(key);
43+
var resFirst = await cache.GetTokenAsync(key, TestContext.Current.CancellationToken);
4444
Assert.Equal(accessTokenFirst, resFirst);
45-
await Task.Delay(1000);
45+
await Task.Delay(1000, TestContext.Current.CancellationToken);
4646

4747

4848
var accessTokenSecond = Guid.NewGuid().ToString();
@@ -54,7 +54,7 @@ public async Task Cache_WorksAsExpected()
5454
ExpiresIn = 1
5555
});
5656

57-
var resSecond = await cache.GetTokenAsync(key);
57+
var resSecond = await cache.GetTokenAsync(key, TestContext.Current.CancellationToken);
5858
Assert.Equal(accessTokenSecond, resSecond);
5959

6060
A.CallTo(() => authClient.GetTokenAsync(A<ClientCredentialsTokenRequest>.Ignored, A<CancellationToken>.Ignored))

0 commit comments

Comments
 (0)