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
<PackageReference Include="Dennisvg.DG.Common.Http.DependencyInjection" Version="2.3.3" />
<PackageVersion Include="Dennisvg.DG.Common.Http.DependencyInjection" Version="2.3.3" />
<PackageReference Include="Dennisvg.DG.Common.Http.DependencyInjection" />
paket add Dennisvg.DG.Common.Http.DependencyInjection --version 2.3.3
#r "nuget: Dennisvg.DG.Common.Http.DependencyInjection, 2.3.3"
#:package Dennisvg.DG.Common.Http.DependencyInjection@2.3.3
#addin nuget:?package=Dennisvg.DG.Common.Http.DependencyInjection&version=2.3.3
#tool nuget:?package=Dennisvg.DG.Common.Http.DependencyInjection&version=2.3.3
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):
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%2FcallbackGetAccessTokenAsync(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).
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).
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 | Versions 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. |
-
.NETStandard 2.0
- Dennisvg.DG.Common.Http (>= 2.3.3)
- Microsoft.Extensions.Http (>= 6.0.0 && < 10.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.