Dennisvg.DG.Common.Http.DependencyInjection 2.3.3

dotnet add package Dennisvg.DG.Common.Http.DependencyInjection --version 2.3.3
                    
NuGet\Install-Package Dennisvg.DG.Common.Http.DependencyInjection -Version 2.3.3
                    
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="Dennisvg.DG.Common.Http.DependencyInjection" Version="2.3.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Dennisvg.DG.Common.Http.DependencyInjection" Version="2.3.3" />
                    
Directory.Packages.props
<PackageReference Include="Dennisvg.DG.Common.Http.DependencyInjection" />
                    
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 Dennisvg.DG.Common.Http.DependencyInjection --version 2.3.3
                    
#r "nuget: Dennisvg.DG.Common.Http.DependencyInjection, 2.3.3"
                    
#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 Dennisvg.DG.Common.Http.DependencyInjection@2.3.3
                    
#: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=Dennisvg.DG.Common.Http.DependencyInjection&version=2.3.3
                    
Install as a Cake Addin
#tool nuget:?package=Dennisvg.DG.Common.Http.DependencyInjection&version=2.3.3
                    
Install as a Cake Tool

dg-common-http

Web access utilities for c#

Fluent HTTP requests

Using the FluentRequest class, which seamlessly integrates in .Net's System.Net.Http environment, it is possible to easily set up a web request by chaining method calls. This allows you to replace the following code

using System.Net.Http;
using Newtonsoft.Json;

var client = new HttpClient();

var formValues = new KeyValuePair<string, string>[]
{
  new KeyValuePair<string, string>("username", "test"),
  new KeyValuePair<string, string>("password", "MyPassword123#")
};
var request = new HttpRequestMessage(HttpMethod.Post, "https://www.example.com/login")
{
  Content = new FormUrlEncodedContent(formValues)
};
request.Headers.Add("Authorization", "Bearer " + token);

var response = await client.SendAsync(request);
var jsonContent = await response.Content.ReadAsStringAsync()
var result = JsonConvert.DeserializeObject<SomeObject>(jsonContent);

With this

using System.Net.Http;
using DG.Common.Http.Fluent;

var client = new HttpClient();

var request = FluentRequest.Post.To("https://www.example.com/login")
  .WithContent(FluentFormContentBuilder
    .With("username", "test")
    .AndWith("password", "MyPassword123#"))
  .WithAuthorizaton(FluentAuthorization.FromBearer(token));

var result = await client.SendAsync(request)
  .DeserializeResponseAsync<SomeObject>();

// Or, if you do not need the HttpResponseMessage, you can use the SendAndDeserializeAsync method directly:
// var result = await client.SendAndDeserializeAsync<SomeObject>(request);

OAuth authorization

Implementing IOAuthLogic

To start using OAuth2 authorization, you first need to implement the four methods in the IOAuthLogic interface (found in the DG.Common.Http.Authorization.OAuth2.Interfaces namespace):

  1. BuildAuthorizationUri(OAuthRequest request)

    This method should create an authorization url to redirect a user to from an authorization request with the given scopes, callback url, and state. for example:

    https://www.api-service.com/authorize?state=abcdefg&scopes=user.read%2Cuser.write&callback-url=https%3A%2F%2Fwww.my-own-app.com%2Fcallback
    
  2. GetAccessTokenAsync(OAuthRequest request, string callBackCode)

    This method should call the external api with a given callBack code, and should return an access token, an expiration date for this access token, and a refresh token (if applicable).

  3. TryRefreshTokenAsync(string refreshToken)

    This method should call the external api with a refresh token, and return a new access token, a new expiration date for this access token, and a new refresh token (if needed).

  4. GetHeaderForToken(string accessToken)

    This method should create an authorization header value for the given access token.

For ease of use, it is recommended to implement IOAuthLogic in a class that has a public, parameterless constructor.

Starting an authorization flow

Starting an authorization flow can be done using any class that implements IOAuthLogic, like the below example.

var scopes = new string[] { "user.read", "email.read", "email.send" };
Uri callbackUri = new Uri("https://www.my-own-app.com/callback");

//ExampleOAuthLogic must implement IOAuthLogic, and expose a parameterless constructor
var flow = OAuthFlow.StartNew<ExampleOAuthLogic>(scopes, callbackUri);

//alternatively, if ExampleOAuthLogic does not have a parameterless constructor
var logic = new ExampleOAuthLogic(clientId, clientSecret);
var flow = OAuthFlow.StartNewFor(logic, scopes, callbackUri);

After receiving the callback code from your callback endpoint, you can continue the authorization flow like this:

await flow.AuthorizationCallbackAsync(callbackCode);

This instance of OAuthFlow is now authorized, and you can now use it to create an authorization header. The OAuthFlow class will automatically refresh the token, if needed (and possible).

var headerValue = await flow.GetAuthorizationHeaderAsync();

var request = new HttpRequestMessage(HttpMethod.Post, "https://api-service.com");
request.Headers.Add("Authorization", headerValue.ToString());

// OAuthFlow can also be used to provide Authorization headers to a FluentRequest directly, like this:
var request = FluentRequest.Post.To("https://api-service.com")
  .WithAuthorization(flow);

Note that an authorization flow can be interrupted and continued at any time, by simply exporting the flow and using the exported data to create a new instance.

var data = flow.Export();

OAuthFlow continued = OAuthFlow.Continue<ExampleOAuthLogic>(data);
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.  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. 
.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 was computed. 
.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

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
2.3.3 194 6/27/2025
2.3.2 188 6/27/2025