Skip to content

coderpatros/dotnet-authenticated-httpclient

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status License: MIT Twitter Follow

Authenticated HttpClient

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.

NuGet Nuget CoderPatros.AuthenticatedHttpClient.AuthorizationHeader
NuGet Nuget CoderPatros.AuthenticatedHttpClient.AzureAd
NuGet Nuget CoderPatros.AuthenticatedHttpClient.AzureAppServiceManagedIdentity
NuGet Nuget CoderPatros.AuthenticatedHttpClient.Basic
NuGet Nuget CoderPatros.AuthenticatedHttpClient.CustomHeader
NuGet Nuget CoderPatros.AuthenticatedHttpClient.QueryStringParameter

Authorization Header Authenticated Http Client Example Usage

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");

Azure AD Authenticated Http Client Example Usage

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");

Azure App Service Managed Identity Authenticated Http Client Example Usage

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");

Basic Authenticated Http Client Example Usage

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");

Custom Header Authenticated Http Client Example Usage

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");

Query String Parameter Authenticated Http Client Example Usage

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");

Chaining Multiple Authentication Methods

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.

About

Authenticated HttpClients for .NET standard

Resources

License

Security policy

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Contributors 2

  •  
  •  

Languages