Skip to content

Commit 0534f74

Browse files
committed
Add support for NetStandard 2.0+
1 parent d68b336 commit 0534f74

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

AutoRefreshTokenHttpMessageHandler/AutoRefreshTokenHttpMessageHandler.csproj

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<LangVersion>latest</LangVersion>
@@ -34,7 +34,14 @@
3434
</ItemGroup>
3535

3636
<ItemGroup>
37+
<PackageReference Include="Microsoft.Bcl.TimeProvider" Version="8.0.1" />
3738
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
39+
<PackageReference Include="PolySharp" Version="1.14.1">
40+
<PrivateAssets>all</PrivateAssets>
41+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
42+
</PackageReference>
43+
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
44+
<PackageReference Include="System.Text.Json" Version="8.0.2" />
3845
</ItemGroup>
3946

4047
</Project>

AutoRefreshTokenHttpMessageHandler/TokenAuthenticationService.cs

+10-5
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ private async Task<Token> RetrieveTokenAsync(string grantType, CancellationToken
3535
{
3636
result.EnsureSuccessStatusCode();
3737
return Token.FromTokenResponse(
38-
await result.Content.ReadFromJsonAsync<TokenResponse>(cancellationToken) ?? throw new HttpRequestException(Translations.INVALID_OR_UNEXPECTED_RESPONSE, null, result.StatusCode),
38+
await result.Content.ReadFromJsonAsync<TokenResponse>(cancellationToken) ?? throw new ExtendedHttpRequestException(Translations.INVALID_OR_UNEXPECTED_RESPONSE, null, result.StatusCode),
3939
_timeprovider.GetUtcNow()
4040
);
4141
}
4242
catch (HttpRequestException ex)
4343
{
44-
var err = await result.Content.ReadFromJsonAsync<ErrorResponse>(cancellationToken) ?? throw new HttpRequestException(Translations.INVALID_OR_UNEXPECTED_RESPONSE, null, result.StatusCode);
45-
throw new HttpRequestException($"[{err.Error}] {err.Description}", ex, result.StatusCode);
44+
var err = await result.Content.ReadFromJsonAsync<ErrorResponse>(cancellationToken) ?? throw new ExtendedHttpRequestException(Translations.INVALID_OR_UNEXPECTED_RESPONSE, null, result.StatusCode);
45+
throw new ExtendedHttpRequestException($"[{err.Error}] {err.Description}", ex, result.StatusCode);
4646
}
4747
}
4848

@@ -76,7 +76,7 @@ private async Task<Token> RefreshTokenAsync(Token token, CancellationToken cance
7676
return await RetrieveTokenAsync(GrantTypes.Refresh, cancellationToken);
7777
}
7878
}
79-
catch (HttpRequestException ex) when (ex.StatusCode is HttpStatusCode.BadRequest or HttpStatusCode.Unauthorized) { }
79+
catch (ExtendedHttpRequestException ex) when (ex.StatusCode is HttpStatusCode.BadRequest or HttpStatusCode.Unauthorized) { }
8080
// Refresh token expired or refresh failed? Try to get a new token
8181
return await RetrieveTokenAsync(GetGrantType(), cancellationToken).ConfigureAwait(false);
8282
}
@@ -99,4 +99,9 @@ private record ErrorResponse
9999
[property: JsonPropertyName("error")] string Error,
100100
[property: JsonPropertyName("error_description")] string Description
101101
);
102-
}
102+
103+
private class ExtendedHttpRequestException(string message, Exception? innerException, HttpStatusCode httpStatusCode) : HttpRequestException(message, innerException)
104+
{
105+
public HttpStatusCode StatusCode { get; private set; } = httpStatusCode;
106+
}
107+
}

0 commit comments

Comments
 (0)