utilizing HmacDemoApi;
var builder = WebApplication.CreateBuilder(args);
// Add companies to the container
// Study extra about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Providers.AddOpenApi();
var app = builder.Construct();
// Configure the HTTP request pipeline.
if (app.Atmosphere.IsDevelopment())
{
app.MapOpenApi();
}
// Allow HMAC safety throughout your complete API pipeline
app.UseMiddleware();
app.MapPost("/api/information", (OrderRequest order, HttpContext context) =>
{
var consumer = context.Person.Identification?.Title; // "Client_App_01"
return Outcomes.Okay($"Product {order.ProductId} from {consumer}");
});
app.Run();
public report OrderRequest(int ProductId, int Amount);
Implement an HMAC authentication shopper in ASP.NET Core
The HMAC authentication shopper must be a Console App venture. For our instance, we’ll mock a easy order service. The shopper builds the request by creating an order occasion, serializing it, and signing the message utilizing the shared helper class. Lastly, the shopper invokes the API by sending the JSON information and the required authentication headers. All of that is proven within the code itemizing given under.
utilizing HmacDemoSharedLibrary;
utilizing System.Textual content;
utilizing System.Textual content.Json;
Console.WriteLine("Press any key to invoke...");
Console.ReadLine();
await HmacClient.SendSecurePostRequestAsync();
Console.WriteLine("Press any key to cease...");
Console.ReadLine();
public class HmacClient
{
non-public static readonly HttpClient shopper = new HttpClient();
public static async Activity SendSecurePostRequestAsync()
{
string url = "https://localhost:44399/api/information";
string path = "/api/information";
var order = new OrderRequest(101, 5);
string jsonBody = JsonSerializer.Serialize(order);
string timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
string technique = "POST";
string payload = $"{technique}n{path}n{timestamp}n{jsonBody}";
string signature = HmacDemoHelper.ComputeHmacSha256(payload);
var request = new HttpRequestMessage(HttpMethod.Put up, url);
request.Content material = new StringContent(jsonBody, Encoding.UTF8, "utility/json");
request.Headers.Add("X-Api-Key", "Client_App_01");
request.Headers.Add("X-Timestamp", timestamp);
request.Headers.Add("X-Signature", signature);
var response = await shopper.SendAsync(request);
string end result = await response.Content material.ReadAsStringAsync();
Console.WriteLine($"Standing Code: {response.StatusCode}");
Console.WriteLine($"Server Response: {end result}");
}
}
public report OrderRequest(int ProductId, int Amount);
HMAC authentication in motion
To run this utility, first set the Net API (HmacDemoApi) and Console App (HmacDemoApiClient) tasks as the beginning tasks in Visible Studio. After you’ve set the Net API and the Console App as the beginning tasks and executed the appliance in Visible Studio, you’ll observe that the Net API venture will launch an internet web page and the Console App will launch a Command window.
Determine 2 under reveals how the shopper app captures the server response and shows it within the Console window.
