DNV.OAuth.Api.HttpClient
1.13.0
See the version list below for details.
dotnet add package DNV.OAuth.Api.HttpClient --version 1.13.0
NuGet\Install-Package DNV.OAuth.Api.HttpClient -Version 1.13.0
<PackageReference Include="DNV.OAuth.Api.HttpClient" Version="1.13.0" />
paket add DNV.OAuth.Api.HttpClient --version 1.13.0
#r "nuget: DNV.OAuth.Api.HttpClient, 1.13.0"
// Install DNV.OAuth.Api.HttpClient as a Cake Addin #addin nuget:?package=DNV.OAuth.Api.HttpClient&version=1.13.0 // Install DNV.OAuth.Api.HttpClient as a Cake Tool #tool nuget:?package=DNV.OAuth.Api.HttpClient&version=1.13.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 | 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 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. |
-
net6.0
- DNV.OAuth.Core (>= 1.13.0)
- 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 | 82 | 10/9/2024 |
1.14.1 | 98 | 9/27/2024 |
1.14.0 | 94 | 9/26/2024 |
1.13.0 | 135 | 8/9/2024 |
1.12.0 | 77 | 7/17/2024 |
1.11.3 | 6,111 | 6/6/2024 |
1.11.1 | 4,275 | 2/8/2024 |
1.11.0 | 202 | 2/8/2024 |
1.10.6 | 10,996 | 12/12/2023 |
1.10.5 | 193 | 12/12/2023 |
1.10.4 | 239 | 12/12/2023 |
1.10.3 | 265 | 12/6/2023 |
1.10.2 | 191 | 12/6/2023 |
1.10.1 | 221 | 12/5/2023 |
1.10.0 | 241 | 11/24/2023 |
1.9.0 | 214 | 11/24/2023 |
1.8.0 | 222 | 11/20/2023 |
1.7.5 | 213 | 11/9/2023 |
1.7.4 | 63,749 | 10/24/2023 |
1.7.3 | 240 | 10/24/2023 |
1.7.0 | 4,316 | 7/26/2023 |
1.6.6 | 286 | 6/26/2023 |
1.6.5 | 261 | 6/20/2023 |
1.6.1 | 249 | 6/8/2023 |
1.6.0 | 2,630 | 6/8/2023 |
1.5.1 | 1,193 | 6/8/2023 |
1.5.0 | 276 | 6/8/2023 |
1.0.0 | 257 | 10/24/2023 |