Stratiteq.Microservices.WebApiClient 1.0.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Stratiteq.Microservices.WebApiClient --version 1.0.0                
NuGet\Install-Package Stratiteq.Microservices.WebApiClient -Version 1.0.0                
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="Stratiteq.Microservices.WebApiClient" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Stratiteq.Microservices.WebApiClient --version 1.0.0                
#r "nuget: Stratiteq.Microservices.WebApiClient, 1.0.0"                
#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 Stratiteq.Microservices.WebApiClient as a Cake Addin
#addin nuget:?package=Stratiteq.Microservices.WebApiClient&version=1.0.0

// Install Stratiteq.Microservices.WebApiClient as a Cake Tool
#tool nuget:?package=Stratiteq.Microservices.WebApiClient&version=1.0.0                

Contains code to more easily make authenticated requests from one Web Api to another, when the APIs require jwt tokens issued from Azure Active Directory (and role based authentication / RBAC). Optimized for use in .NET Core 2.2 or later.

Usage:

  1. Add the Nuget Stratiteq.Microservices.WebApiClient from nuget.org.
  2. Create a client class that handles all communication with a specific Web Api (preferably one class for every Web Api you need to call). E.g. if ServiceA calls ServiceB, create a class in ServiceA called ServiceBClient. Inherit from the base class WebApiClientBase that is accessible through the Nuget package. The following example shows one system making a call to another:
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Stratiteq.Microservices.WebApiClient;

namespace ServiceA
{
    public class ServiceBClient : WebApiClientBase
    {
        public ServiceBClient(HttpClient httpClient, AzureADConfiguration azureADConfiguration)
            : base(httpClient, azureADConfiguration)
        {
        }

        public async Task<CreateServiceBResourceResponse> CreateResourceAsync(CreateServiceBResourceRequest createServiceBResourceRequest)
        {
            HttpClient.DefaultRequestHeaders.Authorization = await GetAuthHeaderValueAsync();

            var response = await HttpClient.PostAsJsonAsync("/v1/resource", createServiceBResourceRequest);

            return JsonConvert.DeserializeObject<CreateServiceBResourceResponse>(await response.Content.ReadAsStringAsync());
        }
    }
}

The base class WebApiClientBase will help you get the token you need with GetAuthHeaderValueAsync() in order to successfully authenticate with ServiceB.

  1. In order to hook up your clients, first create a singleton instance of the AzureADConfiguration class during startup (typically in Startup.cs where you configure your services), e.g:
Services.AddSingleton<AzureADConfiguration>(new AzureADConfiguration
{
    TenantId = config["Security:AzureAD:TenantId"],
    ClientId = config["Security:AzureAD:ClientId"],
    CertificateSubjectName = config["Security:AzureAD:CertificateSubjectName"],
    AADAppIdentifiers =
    {
        { typeof(ServiceBClient), config["ServiceBClient:AADAppIdentifier"] }
    }
});

(The configuration naming is an example only) Note that AADAppIdentifiers is a list, this is because a given service might need to call several different Web Apis. The WebApiClientBase base class knows how to pick the correct identifier based on your typed client class.

  1. The last step is to register your typed client class. Place this code close to (e.g. right under) the above code, e.g.:
Services.AddHttpClient<ServiceBClient>(c =>
{
    c.BaseAddress = new Uri(config["ServiceBClient:BaseUrl"]);
    c.DefaultRequestHeaders.Add(HeaderNames.Accept, "application/json");
});

Here you specify both the base address and accept header, this is to avoid having to repeat this configuration in every method in your client class. This makes it as clean and simple as in the above ServiceBClient example.

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. 
.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
1.0.3 8,787 7/8/2019
1.0.2 639 6/13/2019
1.0.1 1,068 5/16/2019
1.0.0 2,965 5/14/2019