DNV.OAuth.Api.HttpClient
1.10.4
See the version list below for details.
dotnet add package DNV.OAuth.Api.HttpClient --version 1.10.4
NuGet\Install-Package DNV.OAuth.Api.HttpClient -Version 1.10.4
<PackageReference Include="DNV.OAuth.Api.HttpClient" Version="1.10.4" />
paket add DNV.OAuth.Api.HttpClient --version 1.10.4
#r "nuget: DNV.OAuth.Api.HttpClient, 1.10.4"
// Install DNV.OAuth.Api.HttpClient as a Cake Addin #addin nuget:?package=DNV.OAuth.Api.HttpClient&version=1.10.4 // Install DNV.OAuth.Api.HttpClient as a Cake Tool #tool nuget:?package=DNV.OAuth.Api.HttpClient&version=1.10.4
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 | Versions 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. |
-
.NETCoreApp 3.1
- DNV.OAuth.Core (>= 1.10.4)
- Microsoft.IdentityModel.Clients.ActiveDirectory (>= 5.2.9)
- System.Text.Json (>= 6.0.7)
- System.Text.RegularExpressions (>= 4.3.1)
-
net6.0
- DNV.OAuth.Core (>= 1.10.4)
- Microsoft.IdentityModel.Clients.ActiveDirectory (>= 5.2.9)
- System.Text.Json (>= 6.0.7)
- System.Text.RegularExpressions (>= 4.3.1)
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 | 77 | 10/9/2024 |
1.14.1 | 95 | 9/27/2024 |
1.14.0 | 91 | 9/26/2024 |
1.13.0 | 133 | 8/9/2024 |
1.12.0 | 76 | 7/17/2024 |
1.11.3 | 5,813 | 6/6/2024 |
1.11.1 | 3,595 | 2/8/2024 |
1.11.0 | 200 | 2/8/2024 |
1.10.6 | 10,805 | 12/12/2023 |
1.10.5 | 190 | 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 | 60,187 | 10/24/2023 |
1.7.3 | 238 | 10/24/2023 |
1.7.0 | 4,309 | 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,582 | 6/8/2023 |
1.5.1 | 1,189 | 6/8/2023 |
1.5.0 | 275 | 6/8/2023 |
1.0.0 | 255 | 10/24/2023 |