DNV.OAuth.Api.HttpClient 1.7.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package DNV.OAuth.Api.HttpClient --version 1.7.0                
NuGet\Install-Package DNV.OAuth.Api.HttpClient -Version 1.7.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="DNV.OAuth.Api.HttpClient" Version="1.7.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DNV.OAuth.Api.HttpClient --version 1.7.0                
#r "nuget: DNV.OAuth.Api.HttpClient, 1.7.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.
// Install DNV.OAuth.Api.HttpClient as a Cake Addin
#addin nuget:?package=DNV.OAuth.Api.HttpClient&version=1.7.0

// Install DNV.OAuth.Api.HttpClient as a Cake Tool
#tool nuget:?package=DNV.OAuth.Api.HttpClient&version=1.7.0                

OAuth HTTP Client Factory

Provides a factory for producing authenticated HttpClients for API integration via OAuth in .NET.

Developers can use this library to create HttpClient instances which will be pre-authenticated for API requests based on provided configuration.

This package supports two type of credential authentication:

  • User credentials - A user may authenticate by providing a username and password via a UI.
  • Client credentials - A service or application may provide a client id and secret to silently authenticate.

Basic example

1. Configuration

Setup API http client configuration in appsettings.json file:

  {
    "ApiHttpClientOptions": [
      {
        "Name": "userCredentialsClient",
        "Flow": "user-credentials",
        "BaseUri": "<BaseUri>",
        "SubscriptionKey": "<SubscriptionKey>"
      },
      {
        "Name": "clientCredentialsClient",
        "Flow":"client-credentials",
        "BaseUri": "<BaseUri>",
        "SubscriptionKey": "<SubscriptionKey>"
        "OAuthClientOptions": {
          "Authority": "<Authority>",
          "ClientId": "<ClientId>",
          "ClientSecret": "<ClientSecret>",
          "Scopes": [ "<Scope>", "offline_access" ],
          "CallbackPath": "<CallbackPath>"
        }
      }
    ]
  }

The package injects a OAuthHttpClientFactory which is able to provide multiple HttpClients for different purposes. The HttpClients may all be configured through a configuration section in which the individual client configurations are listed with a unique Name which is used to request HttpClients with the corresponding configurations.

The configuration shown above lists 2 HttpClients. The first with name "userCredentialsClient" is an example of a configuration which would honour the signed in user's credentials for the API for which it makes requests. The second with name "clientCredentialsClient" provides configuration for a client which would be authenticated via the client credential flow with a client id and secret to make requests in an API. This configuration would allow us to request either type of HttpClient by requesting it from from the HttpClientFactory by providing one of the two names: "userCredentialsClient" or "clientCredentialsClient" in the method call to the HttpClientFactory.

2. Registration

Call the ServiceCollection extension method AddOAuthHttpClientFactory to register an instance of the OAuthHttpClientFactory in to your project in your Startup.cs file.

The below code is retrieving the configuration from the "ApiHttpClientOptions" section defined in apsettings.json above.

public void ConfigureService(IServiceCollection services)
{
  ...
  services.AddOAuthHttpClientFactory(Congiuration.GetSection("ApiHttpClientOptions").Get<IEnumerable<OAuthHttpClientOptions>>());
  ...
}

If you require a HttpClient applying the user credential flow you should also include the web authentication (AddOidc) and token cache handling (AddDistributedMemoryCache) from the DNV.OAuth.Web package. Include the NuGet package in your project and call the required methods as below:

public void ConfigureService(IServiceCollection services)
{
  ...
  services.AddDistributedMemoryCache();
  ...
  var oidcOptions = new OidcOptions
  {
	Authority = "<Authority>",
	ClientId = "<ClientId>",
	ClientSecret = "<ClientSecret>",
	Scopes = new[] { "<Scope>", "offline_access" },
	ResponseType = OpenIdConnectResponseType.Code
  };
  services.AddOidc(oidcOptions);
  ...
  services.AddOAuthHttpClientFactory(Congiuration.GetSection("ApiHttpClientOptions").Get<IEnumerable<OAuthHttpClientOptions>>());
  ...
}

If you only require HttpClients applying the client credential flow the DNV.OAuth.Web package is not required.

3. Request a client

Resolve IHttpClientFactory to create user-credential or client-credential HttpClient to access web API.

public class ExampleController
{
  private readonly IHttpClientFactory _httpClientFactory;

  public ExampleController(IHttpClientFactory httpClientFactory)
  {
    _httpClientFactory = httpClientFactory;
  }

  public User DoSomethingWithSignInUser(string id)
  {
    var client = _httpClientFactory.CreateWithUserCredentialFlow("userCredentialsClient");
    ...
  }

  public Company DoSomethingWithService(string id)
  {
    var client = _httpClientFactory.CreateWithClientCredentialFlow("clientCredentialsClient");
    ...
  }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on DNV.OAuth.Api.HttpClient:

Package Downloads
DNV.Veracity.Services.Api

Shared supporting library of Veracity My Services API v3 Clients.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.14.2 78 10/9/2024
1.14.1 96 9/27/2024
1.14.0 92 9/26/2024
1.13.0 133 8/9/2024
1.12.0 76 7/17/2024
1.11.3 5,932 6/6/2024
1.11.1 4,044 2/8/2024
1.11.0 200 2/8/2024
1.10.6 10,878 12/12/2023
1.10.5 192 12/12/2023
1.10.4 237 12/12/2023
1.10.3 263 12/6/2023
1.10.2 190 12/6/2023
1.10.1 219 12/5/2023
1.10.0 239 11/24/2023
1.9.0 212 11/24/2023
1.8.0 220 11/20/2023
1.7.5 211 11/9/2023
1.7.4 62,103 10/24/2023
1.7.3 238 10/24/2023
1.7.0 4,314 7/26/2023
1.6.6 284 6/26/2023
1.6.5 260 6/20/2023
1.6.1 247 6/8/2023
1.6.0 2,616 6/8/2023
1.5.1 1,191 6/8/2023
1.5.0 275 6/8/2023
1.0.0 255 10/24/2023