SimpleOAuth2Client.AspNetCore 1.0.0

dotnet add package SimpleOAuth2Client.AspNetCore --version 1.0.0
                    
NuGet\Install-Package SimpleOAuth2Client.AspNetCore -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SimpleOAuth2Client.AspNetCore" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SimpleOAuth2Client.AspNetCore" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="SimpleOAuth2Client.AspNetCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SimpleOAuth2Client.AspNetCore --version 1.0.0
                    
#r "nuget: SimpleOAuth2Client.AspNetCore, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SimpleOAuth2Client.AspNetCore@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SimpleOAuth2Client.AspNetCore&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=SimpleOAuth2Client.AspNetCore&version=1.0.0
                    
Install as a Cake Tool

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)

Vs_Symbols

Next step is to disable “Enable Just My Code” option (Tools --> Options --> Debugging --> General)

Vs_DisbaleMyCode

The last step is to enable Source Server support (Tools --> Options --> Debugging --> General)

Vs_SourceServerSupport

After the successful configuration of Visual Studio 2022 we can set a Breakpoint and start debugging

Vs_Breakpoint

Hint: The first application startup take some time, because Visual Studio try to download the symbol files!

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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