softaware.Authentication.Hmac
1.0.0
See the version list below for details.
dotnet add package softaware.Authentication.Hmac --version 1.0.0
NuGet\Install-Package softaware.Authentication.Hmac -Version 1.0.0
<PackageReference Include="softaware.Authentication.Hmac" Version="1.0.0" />
paket add softaware.Authentication.Hmac --version 1.0.0
#r "nuget: softaware.Authentication.Hmac, 1.0.0"
// Install softaware.Authentication.Hmac as a Cake Addin #addin nuget:?package=softaware.Authentication.Hmac&version=1.0.0 // Install softaware.Authentication.Hmac as a Cake Tool #tool nuget:?package=softaware.Authentication.Hmac&version=1.0.0
softaware.Authentication.Hmac
softaware.Authentication.Hmac.AspNetCore
Provides an AuthenticationHandler
which supports HMAC authentication in an ASP.NET Core project.
Usage:
- Get your HMAC authenticated clients, for example from the
appsettings.json
file. For HMAC authentication, anAppId
and anApiKey
is required for each client which should get access.
var hmacAuthenticatedApps = this.Configuration
.GetSection("Authentication")
.GetSection("HmacAuthenticatedApps")
.Get<HmacAuthenticationClientConfiguration[]>()
.ToDictionary(e => e.AppId, e => e.ApiKey);
{
"Authentication": {
"HmacAuthenticatedApps": [
{
"AppId": "<some-app-id>",
"ApiKey": "<some-api-key>"
}
]
}
}
- Enable HMAC authentication in
Startup.cs
in theConfigureServices
method:
services
.AddHmacAuthentication(HmacAuthenticationDefaults.AuthenticationScheme, "HMAC Authentication", o =>
{
o.MaxRequestAgeInSeconds = HmacAuthenticationDefaults.MaxRequestAgeInSeconds;
o.HmacAuthenticatedApps = hmacAuthenticatedApps;
});
- Add
MemoryCache
(from Microsoft.Extensions.Caching.Memory) inStartup.cs
in theConfigureServices
method. TheMemoryCache
is used by the HMACAuthenticationHandler
to determine replay attacks.
services.AddMemoryCache();
- Enable authentication in
Startup.cs
in theConfigure
method:
app.UseAuthentication();
- Optional: Specify HMAC as the authentication scheme for certain controllers:
[Authorize(AuthenticationSchemes = HmacAuthenticationDefaults.AuthenticationScheme)]
[Route("api/[controller]")]
public class HomeController : Controller
{
// ...
}
softaware.Authentication.Hmac.Client
Provides a DelegatingHandler
for adding an HMAC authorization header to HTTP requests.
Instantiate your HttpClient instance with the ApiKeyDelegatingHandler
:
new HttpClient(new ApiKeyDelegatingHandler(appId, apiKey));
Or in case your WebAPI client is another ASP.NET WebAPI (>= ASP.NET Core 2.1), register your HttpClient
in the Startup.cs
for example as follows:
services.AddTransient(sp => new ApiKeyDelegatingHandler(appId, apiKey));
services
.AddHttpClient("HmacHttpClient")
.AddHttpMessageHandler<ApiKeyDelegatingHandler>();
Generate HMAC AppId and ApiKey
To generate an API Key, the following simple Console Application can be used. This implementation is also provided on .NET Fiddle.
using System.Security.Cryptography;
public class Program
{
public static void Main()
{
Console.WriteLine($"AppID: {Guid.NewGuid()} or <some-speaking-name>");
Console.WriteLine($"ApiKey: {GenerateApiKey()}");
}
private static string GenerateApiKey()
{
using (var cryptoProvider = new RNGCryptoServiceProvider())
{
byte[] secretKeyByteArray = new byte[32]; //256 bit
cryptoProvider.GetBytes(secretKeyByteArray);
return Convert.ToBase64String(secretKeyByteArray);
}
}
}
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. |
.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
- No dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on softaware.Authentication.Hmac:
Package | Downloads |
---|---|
softaware.Authentication.Hmac.Client
A client library for HMAC authentication. |
|
softaware.Authentication.Hmac.AspNetCore
A library which adds support for HMAC authentication in ASP.NET Core projects. |
GitHub repositories
This package is not used by any popular GitHub repositories.