A collection of helpers to create HttpClient instances that automatically handle authentication for you.
They all return HttpClient instances so your favourite extension methods will work too.
CoderPatros.AuthenticatedHttpClient.AuthorizationHeader
CoderPatros.AuthenticatedHttpClient.AzureAd
CoderPatros.AuthenticatedHttpClient.AzureAppServiceManagedIdentity
CoderPatros.AuthenticatedHttpClient.Basic
CoderPatros.AuthenticatedHttpClient.CustomHeader
CoderPatros.AuthenticatedHttpClient.QueryStringParameter
dotnet add package CoderPatros.AuthenticatedHttpClient.AuthorizationHeader
using CoderPatros.AuthenticatedHttpClient;
...
var options = new AuthorizationHeaderAuthenticatedHttpClientOptions
{
Value = "INSERT YOUR AUTHORIZATION HEADER HERE"
};
var client = AuthorizationHeaderAuthenticatedHttpClient.GetClient(options);
var content = await client.GetStringAsync("https://www.example.com");
dotnet add package CoderPatros.AuthenticatedHttpClient.AzureAd
using CoderPatros.AuthenticatedHttpClient;
...
var options = new AzureAdAuthenticatedHttpClientOptions
{
// this is the default value for AadInstance and can be omitted if you don't need to change it
AadInstance = "https://login.microsoftonline.com/{0}",
Tenant = "YOUR AZURE AD TENANT ID (GUID)",
ClientId = "CLIENT APPLICATION ID (GUID)",
AppKey = "CLIENT SECRET",
ResourceId = "APPLICATION ID OF THE SERVICE YOU ARE AUTHENTICATING TO (GUID)"
};
var client = AzureAdAuthenticatedHttpClient.GetClient(options);
var content = await client.GetStringAsync("https://www.example.com");
dotnet add package CoderPatros.AuthenticatedHttpClient.AzureAppServiceManagedIdentity
using CoderPatros.AuthenticatedHttpClient;
...
var options = new AzureAppServiceManagedIdentityAuthenticatedHttpClientOptions
{
ResourceId = "APPLICATION ID OF THE SERVICE YOU ARE AUTHENTICATING TO (GUID)"
};
var client = AzureAppServiceManagedIdentityAuthenticatedHttpClient.GetClient(options);
var content = await client.GetStringAsync("https://www.example.com");
dotnet add package CoderPatros.AuthenticatedHttpClient.Basic
using CoderPatros.AuthenticatedHttpClient;
...
var options = new BasicAuthenticatedHttpClientOptions
{
UserId = "INSERT YOUR USERNAME HERE",
Password = "INSERT YOUR PASSWORD HERE"
};
var client = BasicAuthenticatedHttpClient.GetClient(options);
var content = await client.GetStringAsync("https://www.example.com");
dotnet add package CoderPatros.AuthenticatedHttpClient.CustomHeader
using CoderPatros.AuthenticatedHttpClient;
...
var options = new CustomHeaderAuthenticatedHttpClientOptions
{
Name = "INSERT NAME OF CUSTOM HEADER",
Value = "INSERT VALUE OF CUSTOM HEADER"
};
var client = CustomHeaderAuthenticatedHttpClient.GetClient(options);
var content = await client.GetStringAsync("https://www.example.com");
Or if multiple custom headers are required...
using CoderPatros.AuthenticatedHttpClient;
...
var options = new MultipleCustomHeaderAuthenticatedHttpClientOptions
{
Parameters = new Dictionary<string, string>
{
{ "NAME OF 1ST HEADER", "VALUE OF 1ST HEADER" },
{ "NAME OF 2ND HEADER", "VALUE OF 2ND HEADER" },
...
{ "NAME OF NTH HEADER", "VALUE OF NTH HEADER" }
}
};
var client = CustomHeaderAuthenticatedHttpClient.GetClient(options);
var content = await client.GetStringAsync("https://www.example.com");
dotnet add package CoderPatros.AuthenticatedHttpClient.QueryStringParameter
using CoderPatros.AuthenticatedHttpClient;
...
var options = new QueryStringParameterAuthenticatedHttpClientOptions
{
Name = "INSERT NAME OF QUERY STRING PARAMETER",
Value = "INSERT VALUE OF QUERY STRING PARAMETER"
};
var client = QueryStringParameterAuthenticatedHttpClient.GetClient(options);
var content = await client.GetStringAsync("https://www.example.com");
Or if multiple parameters are required...
using CoderPatros.AuthenticatedHttpClient;
...
var options = new MultipleQueryStringParameterAuthenticatedHttpClientOptions
{
Parameters = new Dictionary<string, string>
{
{ "NAME OF 1ST PARAMETER", "VALUE OF 1ST PARAMETER" },
{ "NAME OF 2ND PARAMETER", "VALUE OF 2ND PARAMETER" },
...
{ "NAME OF NTH PARAMETER", "VALUE OF NTH PARAMETER" }
}
};
var client = QueryStringParameterAuthenticatedHttpClient.GetClient(options);
var content = await client.GetStringAsync("https://www.example.com");
Let's just pretend that you need to interact with an API that requires multiple authentication methods. Just for an example, let's pretend it requires basic authentication, a query string parameter and a custom HTTP header. I know, that would never happen in the real world... oh wait... there's the Whispir API.
dotnet add package CoderPatros.AuthenticatedHttpClient.Basic
dotnet add package CoderPatros.AuthenticatedHttpClient.CustomHeader
dotnet add package CoderPatros.AuthenticatedHttpClient.QueryStringParameter
using CoderPatros.AuthenticatedHttpClient;
...
var basicOptions = new BasicAuthenticatedHttpClientOptions
{
UserId = "WhispirUsername",
Password = "WhispirPassword"
};
var basicClient = BasicAuthenticatedHttpClient.GetClient(basicOptions);
var customHeaderOptions = new CustomHeaderAuthenticatedHttpClientOptions
{
Name = "x-api-key",
Value = "WhispirApiKey"
};
var customHeaderClient = CustomHeaderAuthenticatedHttpClient.GetClient(customHeaderOptions, basicClient);
var queryStringOptions = new QueryStringParameterAuthenticatedHttpClientOptions
{
Name = "apikey",
Value = "WhispirApiKey" // yes, the same one as above :|
};
var client = QueryStringParameterAuthenticatedHttpClient.GetClient(queryStringOptions, customHeaderClient);
var content = await client.GetStringAsync("https://api.<region>.whispir.com/messages");
To make the above simpler I made a WhispirApiHttpClient
class that is available in my dotnet-whispir-api project.