SimpleOAuth2Client.AspNetCore
1.0.0
dotnet add package SimpleOAuth2Client.AspNetCore --version 1.0.0
NuGet\Install-Package SimpleOAuth2Client.AspNetCore -Version 1.0.0
<PackageReference Include="SimpleOAuth2Client.AspNetCore" Version="1.0.0" />
<PackageVersion Include="SimpleOAuth2Client.AspNetCore" Version="1.0.0" />
<PackageReference Include="SimpleOAuth2Client.AspNetCore" />
paket add SimpleOAuth2Client.AspNetCore --version 1.0.0
#r "nuget: SimpleOAuth2Client.AspNetCore, 1.0.0"
#:package SimpleOAuth2Client.AspNetCore@1.0.0
#addin nuget:?package=SimpleOAuth2Client.AspNetCore&version=1.0.0
#tool nuget:?package=SimpleOAuth2Client.AspNetCore&version=1.0.0
About SimpleOAuth2Client.AspNetCore
This repository provide a client for ASP.NET Core based projects that supports parts of the OAuth 2.0 Authorization Framework [RFC6749]
Important Information
At the moment only the authorization grant type "client credentials" is supported. Later the remaining ones are added.
Install NuGet Package
PM> Install-Package -Id SimpleOAuth2Client.AspNetCore -Version 1.0.0-beta
How it works?
The SimpleOAuth2Client project defines a service interface named IOAuth2Client
to request access tokens from a OAuth2 based authorization server. The services is registered with lifetime "Singleton" and can be injected via the ASP.NET Core IoC-Container.
Usage
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// Configure the SimpleOAuth2Client
builder.Services.AddSimpleOAuth2Client(options =>
{
// Configure the options for the client_credentials grant type
options.ClientCredentialOptions.ClientId = <ClientId>;
options.ClientCredentialOptions.ClientSecret = <ClientSecret>;
options.ClientCredentialOptions.TokenEndpoint = <TokenEndpoint>;
});
WebApplication app = builder.Build();
// Inject and use the IOAuth2Client as a service
app.MapGet("/test-access-token-request", async (IOAuth2Client client) =>
{
Result<AccessToken, OAuth2Error> result = await client.RequestAccessToken();
if (result.IsFailure)
{
return Results.BadRequest(result.Error);
}
return Results.Ok(result.Value);
});
app.Run();
Retry mechanism
By default the SimpleOAuth2Client use a retry mechanism to handle transient network errors. A client can override the default values:
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// Configure the SimpleOAuth2Client
builder.Services.AddSimpleOAuth2Client(options =>
{
// Configure the options for the client_credentials grant type
// ...
// Configure the options for the retry algorithm
// Default value is 10
options.RetryOptions.TimeoutPerRetry = 15;
// Default value is 5
options.RetryOptions.RetryAttempts = 3;
// Default value is 2
options.RetryOptions.FirstRetryDelay = 3;
});
Disable Server Certificate validation
For development or testing purpose we can temporally disable the validation of the server certificate:
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// Configure the SimpleOAuth2Client
builder.Services.AddSimpleOAuth2Client(options =>
{
// Configure the options for the client_credentials grant type
// ...
// Disable ServerCertificateValidation for development
options.DisableServerCertificateValidation = true;
});
Please be careful with this feature. Do not use this feature in production!
How to Debug the NuGet-Package
First of all we need to configure Visual Studio 2022 to use Source Link. The related Symbol files are uploaded to the Symbol File repository of NuGet.org.
The first step is to enable the download form the symbol files (Tools --> Options --> Debugging --> Symbols)
Next step is to disable “Enable Just My Code” option (Tools --> Options --> Debugging --> General)
The last step is to enable Source Server support (Tools --> Options --> Debugging --> General)
After the successful configuration of Visual Studio 2022 we can set a Breakpoint and start debugging
Hint: The first application startup take some time, because Visual Studio try to download the symbol files!
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- CSharpFunctionalExtensions (>= 2.37.0)
- FluentValidation (>= 11.5.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Hosting (>= 7.0.1)
- Microsoft.Extensions.Http (>= 7.0.0)
- Microsoft.Extensions.Http.Polly (>= 7.0.4)
- Polly.Contrib.WaitAndRetry (>= 1.1.1)
-
net7.0
- CSharpFunctionalExtensions (>= 2.37.0)
- FluentValidation (>= 11.5.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Hosting (>= 7.0.1)
- Microsoft.Extensions.Http (>= 7.0.0)
- Microsoft.Extensions.Http.Polly (>= 7.0.4)
- Polly.Contrib.WaitAndRetry (>= 1.1.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.0.0 | 259 | 4/12/2023 |
1.0.0-beta | 196 | 3/27/2023 |
1.0.0-alpha | 196 | 3/13/2023 |