aemarcoToolboxWeb 10.3.2
dotnet add package aemarcoToolboxWeb --version 10.3.2
NuGet\Install-Package aemarcoToolboxWeb -Version 10.3.2
<PackageReference Include="aemarcoToolboxWeb" Version="10.3.2" />
<PackageVersion Include="aemarcoToolboxWeb" Version="10.3.2" />
<PackageReference Include="aemarcoToolboxWeb" />
paket add aemarcoToolboxWeb --version 10.3.2
#r "nuget: aemarcoToolboxWeb, 10.3.2"
#:package aemarcoToolboxWeb@10.3.2
#addin nuget:?package=aemarcoToolboxWeb&version=10.3.2
#tool nuget:?package=aemarcoToolboxWeb&version=10.3.2
ToolboxWeb
Successor to WebTools, which is being discontinued. Unlike WebTools, this project is not meant to become a single catch-all again — each piece here should earn its place; anything that only one consumer needs, or that's small enough to inline, belongs in that consumer instead.
Table of Contents
Authentication
Adds an authentication scheme that unconditionally succeeds and stamps the caller as "Anonymous" — it performs no real check. This exists for authorization policies where identity itself isn't the gate (e.g. LAN-IP-based policies below): ASP.NET Core's pipeline always requires some authentication scheme to produce a ClaimsPrincipal before a policy's requirements run, even when the actual decision happens entirely in a requirement handler. Never register it as the default scheme.
services.AddAuthentication(...)
.AddAnonymousScheme("Anon");
Authorization
LAN IP Authorization
Restricts access to requests originating from trusted IP ranges.
- Loopback addresses (
127.0.0.1,::1) are always allowed. - RFC-1918 private ranges (
10.0.0.0/8,172.16.0.0/12,192.168.0.0/16) are trusted by default.
Register one or more named policies:
// Default: allows all RFC-1918 ranges (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16)
services.AddLanIpAddressPolicy("LanOnly");
// Custom subnets
services.AddLanIpAddressPolicy("LanOnly", opts =>
{
opts.LocalSubnets = ["192.168.20.0/24"];
});
// Exact IPs — clears subnets and adds /32 (IPv4) or /128 (IPv6) entries; throws on invalid input
services.AddLanIpAddressPolicy("LanOnly", opts =>
{
opts.WithIpAddresses("192.168.1.10", "192.168.1.11");
});
// Enable hairpin NAT support (allow own public IP)
services.AddLanIpAddressPolicy("LanOnly", opts =>
{
opts.AllowOwnPublicIp = true;
});
// With a specific authentication scheme
services.AddLanIpAddressPolicy("Bearer", "LanOnly");
AllowOwnPublicIp resolves the server's own public IP via IPublicIpService, trying several external lookup services in order and caching the result. If every source fails, resolution returns null and the hairpin check is skipped for that request rather than failing open — it never auto-allows on a resolution failure.
Allowed Clients
Adds a policy which requires the client_id claim to match one of the given client IDs.
services.AddAllowedClientsPolicy("clientB", "clientB");
Policy documentation can be found at https://docs.microsoft.com/en-us/aspnet/core/security/authorization/policies
Great read around DefaultPolicy and FallbackPolicy https://scottsauber.com/2020/01/20/globally-require-authenticated-users-by-default-using-fallback-policies-in-asp-net-core
Read about Policy Conventions https://andrewlock.net/setting-global-authorization-policies-using-the-defaultpolicy-and-the-fallbackpolicy-in-aspnet-core-3
Configuration
X509 Certificate Settings
X509CertificateSettingsBase — POCO (Authority, Audience, Path, Pwd) for certificate-backed JWT bearer setups. Authority and Audience are independent settings — don't reuse one for both, or you lose the distinction between "who signed this token" and "who it's meant for". Call ApplyTo(TokenValidationParameters) to load the cert and wire up ValidIssuer/ValidAudience/IssuerSigningKey in one go:
services.AddOptions<JwtBearerOptions>(scheme)
.Configure<CertSettings>((options, certSettings) =>
certSettings.ApplyTo(options.TokenValidationParameters));
If you are using this (unchanged), nothing extra is needed. If you extend, you need to roll your own mapper. To use together with ToolboxAppOptions, just do:
public class CertSettings : X509CertificateSettingsBase, ISettingsBase;
OpenID Connect Settings
OpenIdConnectSettings — settings POCO that maps directly onto OpenIdConnectOptions via the generated ApplyTo() extension. If you are using this (unchanged), nothing extra is needed. If you extend, you need to roll your own mapper. To use together with ToolboxAppOptions, just do:
public class OidcSettings : OpenIdConnectSettings, ISettingsBase;
services.AddOptions<OpenIdConnectOptions>(scheme)
.Configure<OidcSettings>((options, oidcSettings) => oidcSettings.ApplyTo(options));
Extensions
ContextExtensions — HttpContext helpers for reconstructing the request's root/base/absolute URL (GetRootPath/GetBasePath/GetAbsolutePath) and reading the caller's bearer token (GetAccessToken). GetAccessToken returns just the token — the "Bearer " scheme prefix is stripped — or null if the Authorization header isn't a bearer token at all.
Logging
SetupSerilogging
Precedence — code defaults < appsettings.json < the configure callback — is deliberate, and achieved differently per Serilog API since Serilog isn't symmetric here:
- Enrichment properties use first-set-wins semantics, so they're applied before
ReadFrom.Configuration."app"/"EnvironmentApplicationName"are derived fromapp.Environment.ApplicationNamerather than left config-overridable, since that's reconfigured at the builder level, not through Serilog. MinimumLevel.Overrideuses last-set-wins semantics, soReadFrom.Configurationruns after the code-level"Microsoft": Informationdefault, andconfigureruns last of all. This is namespace-prefix matching, not glob —"Microsoft*"is a literal string matching nothing.
Same implementation as ToolboxConsole.ConsoleAppExtensions.SetupSerilogging, just on WebApplicationBuilder for the web tier instead of the console tier.
Middleware
Bearer To Header
Adds a middleware, which moves an access_token provided as query parameter, to an Authorization header for a Bearer.
app.UseBearerToHeaderMiddleware(); //move bearer from url to header before routing
Exception Middleware
Catches exceptions and writes a JSON ErrorResponse. BadRequestException maps to 400 with its message; anything else maps to 500 with a deliberately generic message — the real exception is logged, never sent to the client.
app.UseExceptionMiddleware(); // production: catches everything
app.UseDeveloperExceptionMiddleware(); // dev: BadRequestException still returns 400 JSON, everything else goes to ASP.NET's developer exception page
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- aemarcoExtensions (>= 10.3.2)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 10.0.13)
- Microsoft.AspNetCore.Authentication.OpenIdConnect (>= 10.0.13)
- Serilog.AspNetCore (>= 10.0.0)
- Serilog.Enrichers.Environment (>= 3.0.1)
- Serilog.Enrichers.Thread (>= 4.0.0)
- Serilog.Extensions.Hosting (>= 10.0.0)
- Serilog.Settings.Configuration (>= 10.0.1)
- Serilog.Sinks.Async (>= 2.1.0)
- Serilog.Sinks.Console (>= 6.1.1)
- Serilog.Sinks.File (>= 7.0.0)
- Serilog.Sinks.Seq (>= 9.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.