Skip to content

Commit 3862101

Browse files
committed
feat: .net 6
1 parent 4f2f7df commit 3862101

File tree

15 files changed

+79
-136
lines changed

15 files changed

+79
-136
lines changed

samples/NetDevPack.Security.JwtExtensions.ApiAuthenticator/NetDevPack.Security.JwtExtensions.ApiAuthenticator.csproj

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,20 @@
66

77

88
<ItemGroup>
9-
<PackageReference Include="Bogus" Version="33.0.2" />
10-
<PackageReference Include="NetDevPack.Security.JwtSigningCredentials.AspNetCore" Version="5.0.6" />
11-
<PackageReference Include="NetDevPack.Security.JwtSigningCredentials.Store.FileSystem" Version="5.0.6" />
12-
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.11.0" />
9+
<PackageReference Include="Bogus" Version="34.0.2" />
10+
<PackageReference Include="NetDevPack.Security.Jwt.AspNetCore" Version="6.0.5" />
11+
<PackageReference Include="NetDevPack.Security.Jwt.Core" Version="6.0.5" />
12+
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.17.0" />
1313
</ItemGroup>
1414

15+
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
16+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.24" />
17+
</ItemGroup>
18+
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
19+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.16" />
20+
</ItemGroup>
21+
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
22+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.4" />
23+
</ItemGroup>
1524

1625
</Project>

samples/NetDevPack.Security.JwtExtensions.ApiAuthenticator/Services/DummyAuthenticationService.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
using Bogus;
2-
using Microsoft.AspNetCore.Http;
3-
using Microsoft.Extensions.Configuration;
4-
using Microsoft.IdentityModel.Tokens;
5-
using NetDevPack.Security.JwtSigningCredentials.Interfaces;
6-
using System;
1+
using System;
72
using System.IdentityModel.Tokens.Jwt;
83
using System.Security.Claims;
94
using System.Threading.Tasks;
5+
using ApiAuthenticator.Services;
6+
using Bogus;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.IdentityModel.Tokens;
10+
using NetDevPack.Security.Jwt.Core.Interfaces;
1011

11-
namespace ApiAuthenticator.Services
12+
namespace NetDevPack.Security.JwtExtensions.ApiAuthenticator.Services
1213
{
1314
public class AuthenticationService : IAuthenticationService
1415
{
1516
private readonly IConfiguration _configuration;
16-
private readonly IJsonWebKeySetService _jsonWebKeySetService;
17+
private readonly IJwtService _jsonWebKeySetService;
1718
private readonly IHttpContextAccessor _httpContextAccessor;
1819

19-
public AuthenticationService(IConfiguration configuration, IJsonWebKeySetService jsonWebKeySetService, IHttpContextAccessor httpContextAccessor)
20+
public AuthenticationService(IConfiguration configuration, IJwtService jsonWebKeySetService, IHttpContextAccessor httpContextAccessor)
2021
{
2122
_configuration = configuration;
2223
_jsonWebKeySetService = jsonWebKeySetService;
@@ -28,7 +29,7 @@ public async ValueTask<object> GenerateToken()
2829

2930
var faker = new Faker();
3031
var tokenHandler = new JwtSecurityTokenHandler();
31-
var key = _jsonWebKeySetService.GetCurrent();
32+
var key = await _jsonWebKeySetService.GetCurrentSigningCredentials();
3233

3334
var tokenDescriptor = new SecurityTokenDescriptor
3435
{

samples/NetDevPack.Security.JwtExtensions.ApiAuthenticator/Startup.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using Microsoft.Extensions.Configuration;
44
using Microsoft.Extensions.DependencyInjection;
55
using Microsoft.Extensions.Hosting;
6-
using NetDevPack.Security.JwtSigningCredentials.AspNetCore;
76
using System.IO;
87

98
namespace ApiAuthenticator
@@ -25,7 +24,7 @@ public void ConfigureServices(IServiceCollection services)
2524
{
2625
services.AddControllers();
2726
services.AddHttpContextAccessor();
28-
services.AddJwksManager().PersistKeysToFileSystem(new DirectoryInfo(_env.ContentRootPath));
27+
services.AddJwksManager().PersistKeysInMemory();
2928
}
3029

3130
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

samples/NetDevPack.Security.JwtExtensions.ApiClient/NetDevPack.Security.JwtExtensions.ApiClient.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.1.0" />
1414
</ItemGroup>
1515
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
16-
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.15" />
16+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.24" />
1717
</ItemGroup>
1818
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
19-
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.11" />
19+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.16" />
2020
</ItemGroup>
2121
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
22-
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.0-rc.2.21480.10" />
22+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.4" />
2323
</ItemGroup>
2424

2525
</Project>

src/NetDevPack.Security.JwtExtensions/JwkOptions.cs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,16 @@ namespace NetDevPack.Security.JwtExtensions
44
{
55
public class JwkOptions
66
{
7-
public JwkOptions(string jwksUri)
7+
public JwkOptions(string jwksUri, string issuer = null, TimeSpan? cacheTime = null, string audience = null)
88
{
99
JwksUri = new Uri(jwksUri);
10-
Issuer = $"{JwksUri.Scheme}://{JwksUri.Authority}";
11-
KeepFor = TimeSpan.FromMinutes(15);
10+
Issuer = issuer ?? $"{JwksUri.Scheme}://{JwksUri.Authority}";
11+
KeepFor = cacheTime ?? TimeSpan.FromMinutes(15);
12+
Audience = audience;
1213
}
13-
14-
public JwkOptions(string jwksUri, TimeSpan cacheTime)
15-
{
16-
JwksUri = new Uri(jwksUri);
17-
Issuer = $"{JwksUri.Scheme}://{JwksUri.Authority}";
18-
KeepFor = cacheTime;
19-
}
20-
21-
public JwkOptions(string jwksUri, string issuer, TimeSpan cacheTime)
22-
{
23-
JwksUri = new Uri(jwksUri);
24-
Issuer = issuer;
25-
KeepFor = cacheTime;
26-
}
27-
public string Issuer { get; private set; }
28-
public Uri JwksUri { get; private set; }
29-
public TimeSpan KeepFor { get; private set; }
30-
14+
public string Issuer { get; }
15+
public Uri JwksUri { get; }
16+
public TimeSpan KeepFor { get; }
17+
public string Audience { get; }
3118
}
3219
}

src/NetDevPack.Security.JwtExtensions/JwksExtension.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,15 @@ public static void SetJwksOptions(this JwtBearerOptions options, JwkOptions jwkO
2121
jwkOptions.JwksUri.OriginalString,
2222
new JwksRetriever(),
2323
new HttpDocumentRetriever(httpClient) { RequireHttps = options.RequireHttpsMetadata });
24+
2425
options.TokenValidationParameters.ValidateAudience = false;
2526
options.TokenValidationParameters.ValidIssuer = jwkOptions.Issuer;
27+
28+
if (!string.IsNullOrEmpty(jwkOptions.Audience))
29+
{
30+
options.TokenValidationParameters.ValidateAudience = true;
31+
options.TokenValidationParameters.ValidAudience = jwkOptions.Audience;
32+
}
2633
}
2734
}
2835
}

src/NetDevPack.Security.JwtExtensions/JwksRetriever.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading;
1+
using System;
2+
using System.Threading;
23
using System.Threading.Tasks;
34
using Microsoft.IdentityModel.Logging;
45
using Microsoft.IdentityModel.Protocols;
@@ -29,7 +30,8 @@ public static async Task<OpenIdConnectConfiguration> GetAsync(string address, ID
2930
if (retriever == null)
3031
throw LogHelper.LogArgumentNullException(nameof(retriever));
3132

32-
var doc = await retriever.GetDocumentAsync(address, cancel).ConfigureAwait(false);
33+
IdentityModelEventSource.ShowPII = true;
34+
var doc = await retriever.GetDocumentAsync(address, cancel);
3335
LogHelper.LogVerbose("IDX21811: Deserializing the string: '{0}' obtained from metadata endpoint into openIdConnectConfiguration object.", doc);
3436
var jwks = new JsonWebKeySet(doc);
3537
var openIdConnectConfiguration = new OpenIdConnectConfiguration()

src/NetDevPack.Security.JwtExtensions/NetDevPack.Security.JwtExtensions.csproj

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,20 @@
1515
</PropertyGroup>
1616

1717
<ItemGroup>
18-
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.13.1" />
18+
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.17.0" />
19+
<PackageReference Include="System.Text.Json" Version="6.0.3" />
1920
</ItemGroup>
2021

2122
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
22-
<PackageReference Include="System.Text.Json" Version="5.0.2" />
23-
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.1.0" />
23+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.2.0" />
2424
</ItemGroup>
2525
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
26-
<PackageReference Include="System.Text.Json" Version="5.0.2" />
27-
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.15" />
26+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.24" />
2827
</ItemGroup>
2928
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
30-
<PackageReference Include="System.Text.Json" Version="5.0.2" />
31-
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.11" />
29+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.16" />
3230
</ItemGroup>
3331
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
34-
<PackageReference Include="System.Text.Json" Version="6.0.0-rc.2.21480.5" />
35-
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.0-rc.2.21480.10" />
32+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.4" />
3633
</ItemGroup>
3734
</Project>

tests/NetDevPack.Security.JwtExtensions.ApiTests/NetDevPack.Security.JwtExtensions.ApiTests.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.1.0" />
1414
</ItemGroup>
1515
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
16-
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.15" />
16+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.24" />
1717
</ItemGroup>
1818
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
19-
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.11" />
19+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.16" />
2020
</ItemGroup>
2121
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
22-
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.0-rc.2.21480.10" />
22+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.4" />
2323
</ItemGroup>
2424
</Project>

tests/NetDevPack.Security.JwtExtensions.ApiTests/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void ConfigureServices(IServiceCollection services)
2525
.AddJwtBearer(options =>
2626
{
2727
options.IncludeErrorDetails = true; // <- great for debugging
28-
options.SetJwksOptions(new JwkOptions("https://localhost:5001/jwks"));
28+
options.SetJwksOptions(new JwkOptions("https://localhost:5001/jwks", audience: "jwt-test"));
2929
});
3030
}
3131

0 commit comments

Comments
 (0)