DNV.OAuth.Api.HttpClient 1.11.1

dotnet add package DNV.OAuth.Api.HttpClient --version 1.11.1
NuGet\Install-Package DNV.OAuth.Api.HttpClient -Version 1.11.1
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.11.1" />
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.11.1
#r "nuget: DNV.OAuth.Api.HttpClient, 1.11.1"
#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.11.1

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

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 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 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 netcoreapp3.1 is compatible. 
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.11.1 145 2/8/2024
1.11.0 158 2/8/2024
1.10.6 7,169 12/12/2023
1.10.5 171 12/12/2023
1.10.4 214 12/12/2023
1.10.3 241 12/6/2023
1.10.2 164 12/6/2023
1.10.1 192 12/5/2023
1.10.0 213 11/24/2023
1.9.0 185 11/24/2023
1.8.0 196 11/20/2023
1.7.5 185 11/9/2023
1.7.4 24,056 10/24/2023
1.7.3 214 10/24/2023
1.7.0 3,923 7/26/2023
1.6.6 259 6/26/2023
1.6.5 232 6/20/2023
1.6.1 224 6/8/2023
1.6.0 2,114 6/8/2023
1.5.1 1,028 6/8/2023
1.5.0 247 6/8/2023
1.0.0 206 10/24/2023