Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
Rework to support both .NET 6 and 8
Browse files Browse the repository at this point in the history
  • Loading branch information
josephdecock committed Nov 20, 2023
1 parent 930ca3c commit 081935d
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 29 deletions.
35 changes: 35 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project>
<PropertyGroup Condition=" '$(TargetFramework)' == 'net8.0'">
<FrameworkVersion>8.0.0</FrameworkVersion>
<ExtensionsVersion>8.0.0</ExtensionsVersion>
<WilsonVersion>7.0.3</WilsonVersion>
<IdentityServerVersion>7.0.0-preview.2</IdentityServerVersion>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net6.0'">
<FrameworkVersion>6.0.0</FrameworkVersion>
<ExtensionsVersion>6.0.0</ExtensionsVersion>
<WilsonVersion>6.15.0</WilsonVersion>
<IdentityServerVersion>6.3.6</IdentityServerVersion>
</PropertyGroup>


<ItemGroup>
<!-- ASP.NET -->
<PackageReference Update="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="$(FrameworkVersion)" />
<PackageReference Update="Microsoft.AspNetCore.Authentication.JwtBearer" Version="$(FrameworkVersion)" />
<PackageReference Update="Microsoft.AspNetCore.TestHost" Version="$(FrameworkVersion)" />
<PackageReference Update="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="$(FrameworkVersion)" />

<!-- Microsoft Extensions -->
<PackageReference Update="Microsoft.Extensions.Caching.Abstractions" Version="$(ExtensionsVersion)" />
<PackageReference Update="Microsoft.Extensions.Logging.Abstractions" Version="$(ExtensionsVersion)" />
<PackageReference Update="Microsoft.Extensions.Options" Version="$(ExtensionsVersion)" />
<PackageReference Update="Microsoft.Extensions.Http" Version="$(ExtensionsVersion)" />
<PackageReference Update="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(ExtensionsVersion)" />

<!-- Wilson -->
<PackageReference Update="System.IdentityModel.Tokens.Jwt" Version="$(WilsonVersion)" />

</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "6.0.100",
"version": "8.0.100",
"rollForward": "latestMajor",
"allowPrerelease": false
}
Expand Down
10 changes: 5 additions & 5 deletions samples/Web/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task<IActionResult> CallApiAsUserManual()
var client = _httpClientFactory.CreateClient();
client.SetToken(token.AccessTokenType!, token.AccessToken!);

var response = await client.GetStringAsync("https://demo.duendesoftware.com/api/test");
var response = await client.GetStringAsync("https://demo.duendesoftware.com/api/dpop/test");
ViewBag.Json = PrettyPrint(response);

return View("CallApi");
Expand All @@ -45,7 +45,7 @@ public async Task<IActionResult> CallApiAsUserExtensionMethod()
var client = _httpClientFactory.CreateClient();
client.SetToken(token.AccessTokenType!, token.AccessToken!);

var response = await client.GetStringAsync("https://demo.duendesoftware.com/api/test");
var response = await client.GetStringAsync("https://demo.duendesoftware.com/api/dpop/test");
ViewBag.Json = PrettyPrint(response);

return View("CallApi");
Expand Down Expand Up @@ -76,7 +76,7 @@ public async Task<IActionResult> CallApiAsUserResourceIndicator()
var client = _httpClientFactory.CreateClient();
client.SetToken(token.AccessTokenType!, token.AccessToken!);

var response = await client.GetStringAsync("https://demo.duendesoftware.com/api/test");
var response = await client.GetStringAsync("https://demo.duendesoftware.com/api/dpop/test");

ViewBag.Json = PrettyPrint(response);
return View("CallApi");
Expand All @@ -90,7 +90,7 @@ public async Task<IActionResult> CallApiAsClientExtensionMethod()
var client = _httpClientFactory.CreateClient();
client.SetToken(token.AccessTokenType!, token.AccessToken!);

var response = await client.GetStringAsync("https://demo.duendesoftware.com/api/test");
var response = await client.GetStringAsync("https://demo.duendesoftware.com/api/dpop/test");

ViewBag.Json = PrettyPrint(response);
return View("CallApi");
Expand All @@ -103,7 +103,7 @@ public async Task<IActionResult> CallApiAsClientResourceIndicator()
var client = _httpClientFactory.CreateClient();
client.SetToken(token.AccessTokenType!, token.AccessToken!);

var response = await client.GetStringAsync("https://demo.duendesoftware.com/api/test");
var response = await client.GetStringAsync("https://demo.duendesoftware.com/api/dpop/test");

ViewBag.Json = PrettyPrint(response);
return View("CallApi");
Expand Down
12 changes: 6 additions & 6 deletions samples/Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,30 @@ internal static WebApplication ConfigureServices(this WebApplicationBuilder buil
{
// if you uncomment this line, then be sure to change the URL for the "user_client"
// to include "dpop/" at the end, since that's the DPoP enabled API path
//options.DPoPJsonWebKey = jwk;
options.DPoPJsonWebKey = jwk;
});

// registers HTTP client that uses the managed user access token
builder.Services.AddUserAccessTokenHttpClient("user_client",
configureClient: client => {
client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/");
//client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/dpop/");
//client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/");
client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/dpop/");
});

// registers HTTP client that uses the managed client access token
builder.Services.AddClientAccessTokenHttpClient("client",
configureClient: client => { client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/"); });
configureClient: client => { client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/dpop/"); });

// registers a typed HTTP client with token management support
builder.Services.AddHttpClient<TypedUserClient>(client =>
{
client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/");
client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/dpop/");
})
.AddUserAccessTokenHandler();

builder.Services.AddHttpClient<TypedClientClient>(client =>
{
client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/");
client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/dpop/");
})
.AddClientAccessTokenHandler();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void Configure(ClientCredentialsClient options)
throw new System.NotImplementedException();
}

public void Configure(string name, ClientCredentialsClient options)
public void Configure(string? name, ClientCredentialsClient options)
{
if (name == "demo.jwt")
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

Expand All @@ -11,7 +11,8 @@

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.0" />

<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" />

<PackageReference Include="MinVer" Version="4.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ public class UserAccessAccessTokenManagementService : IUserTokenManagementServic
{
private readonly IUserTokenRequestSynchronization _sync;
private readonly IUserTokenStore _userAccessTokenStore;
#if NET8_0_OR_GREATER
private readonly TimeProvider _clock;
#else
private readonly ISystemClock _clock;
#endif
private readonly UserTokenManagementOptions _options;
private readonly IUserTokenEndpointService _tokenEndpointService;
private readonly ILogger<UserAccessAccessTokenManagementService> _logger;
Expand All @@ -36,7 +40,11 @@ public class UserAccessAccessTokenManagementService : IUserTokenManagementServic
public UserAccessAccessTokenManagementService(
IUserTokenRequestSynchronization sync,
IUserTokenStore userAccessTokenStore,
#if NET8_0_OR_GREATER
TimeProvider clock,
#else
ISystemClock clock,
#endif
IOptions<UserTokenManagementOptions> options,
IUserTokenEndpointService tokenEndpointService,
ILogger<UserAccessAccessTokenManagementService> logger)
Expand Down Expand Up @@ -92,7 +100,12 @@ public async Task<UserToken> GetAccessTokenAsync(
}

var dtRefresh = userToken.Expiration.Subtract(_options.RefreshBeforeExpiration);
if (dtRefresh < _clock.GetUtcNow() || parameters.ForceRenewal || needsRenewal)
#if NET8_0_OR_GREATER
var utcNow = _clock.GetUtcNow();
#else
var utcNow = _clock.UtcNow;
#endif
if (dtRefresh < utcNow || parameters.ForceRenewal || needsRenewal)
{
_logger.LogDebug("Token for user {user} needs refreshing.", userName);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

Expand All @@ -16,12 +16,11 @@
<PackageReference Include="MinVer" Version="4.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />

<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Options" />
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions test/Tests/Framework/TestLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public void Dispose()
}

public IDisposable BeginScope<TState>(TState state)
#if NET8_0_OR_GREATER
where TState : notnull
#endif
{
return this;
}
Expand Down
10 changes: 5 additions & 5 deletions test/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -11,11 +11,11 @@
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />

<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer"/>
<PackageReference Include="Microsoft.AspNetCore.TestHost"/>

<PackageReference Include="Duende.IdentityServer" Version="7.0.0-preview.2" />
<PackageReference Include="Duende.IdentityServer" Version="$(IdentityServerVersion)" />

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand Down

0 comments on commit 081935d

Please sign in to comment.