Likvido.Http 1.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Likvido.Http --version 1.2.0
                    
NuGet\Install-Package Likvido.Http -Version 1.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Likvido.Http" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Likvido.Http" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Likvido.Http" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Likvido.Http --version 1.2.0
                    
#r "nuget: Likvido.Http, 1.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Likvido.Http@1.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Likvido.Http&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Likvido.Http&version=1.2.0
                    
Install as a Cake Tool

Likvido.Http GitHub Workflow Status Nuget

HTTP client helpers for Likvido services. It depends only on Microsoft.Extensions.Http and Microsoft.Extensions.Logging.Abstractions, so web projects, robots and other libraries can all use it.

Guarded clients

Use a guarded client for every server-side fetch of a URL that comes from outside data, such as a download link in a third party's reply. Register it with AddGuardedHttpClient instead of AddHttpClient:

services.AddGuardedHttpClient<IPdfService, PdfService>(
        new OutboundRequestPolicy(
            ["https://api.pdf.co", "https://pdf-temp-files.s3.us-west-2.amazonaws.com"],
            maxResponseBytes: 20 * 1024 * 1024))
    .ConfigureHttpClient(client => client.Timeout = TimeSpan.FromSeconds(30))
    .AddStandardResilienceHandler();

A guarded client:

  • sends a request only to an origin its policy allows, and refuses any other before anything is sent;
  • opens a connection only to a public IP address. It checks every address the host resolves to and connects to the address it checked, so DNS rebinding cannot swap in a private address;
  • follows no redirects, and refuses a redirect response;
  • refuses a response whose Content-Length is over the cap, and stops reading any body once it passes the cap. When a response is decompressed, its Content-Length is gone, and the decompressed bytes read are counted;
  • uses no proxy, no cookies and no HTTP/3.

The policy

  • Origins are exact: a scheme, a host and, when it is not the default, a port, such as https://api.pdf.co. There are no wildcards. Listing https://example.com does not allow http://example.com; an http origin is allowed only when it is listed itself.

  • maxResponseBytes caps the response body. With automatic decompression, it caps the decompressed body.

  • AutomaticDecompression is off by default. Set it for a host that sends compressed responses:

    new OutboundRequestPolicy(["http://regnskaber.virk.dk"], maxResponseBytes: 1024 * 1024)
    {
        AutomaticDecompression = DecompressionMethods.GZip
    }
    
  • MaxConnectionsPerServer limits how many connections the client opens to one server at a time. There is no limit by default. Set it for a client whose URLs an outsider chooses, so one slow host cannot tie up an unbounded number of sockets.

Any https origin

When the hosts cannot be listed in advance, such as an OAuth client's metadata document at a URL the client chooses, allow any https origin instead:

services.AddGuardedHttpClient(
        "ClientMetadata",
        OutboundRequestPolicy.ForAnyHttpsOrigin(maxResponseBytes: 64 * 1024, maxConnectionsPerServer: 8))
    .ConfigureHttpClient(client => client.Timeout = TimeSpan.FromSeconds(5));

Such a client sends a request to any https URL on the default port (443). It refuses an http URL and an https URL with another port as UnlistedOrigin, before anything is sent. Every other check still applies: the connection goes only to a public address, redirects are refused and the response is capped. Automatic decompression is off.

A policy can also be built from the service provider, for example from configuration:

services.AddGuardedHttpClient<IAttachmentDownloader, AttachmentDownloader>(serviceProvider =>
    new OutboundRequestPolicy(
        [serviceProvider.GetRequiredService<IOptions<StorageOptions>>().Value.AccountOrigin],
        maxResponseBytes: 25 * 1024 * 1024));

There are overloads for a typed client with an implementation (AddGuardedHttpClient<TClient, TImplementation>), a typed client without one (AddGuardedHttpClient<TClient>) and a named client (AddGuardedHttpClient(name, policy)). Each returns the client's IHttpClientBuilder.

Refusals

A refused request throws OutboundRequestRefusedException. Its Reason is one of UnlistedOrigin (the policy does not allow the origin), NonPublicAddress, ResponseTooLarge and Redirect, and its Origin is the scheme, the host and any non-default port. It does not derive from HttpRequestException, so a resilience handler does not retry it.

The guard logs each refusal once, at Warning, in the category Likvido.Http.OutboundRequestGuard, with the client's name, the origin, and the reason. It never logs the path or query string, because a signed storage link carries its credential in the query string. Neither does the exception's message.

Adding handlers

The guard is the client's primary handler, so it runs inside every handler the caller adds, such as AddStandardResilienceHandler. It sees each request exactly as it is sent.

Do not call ConfigurePrimaryHttpMessageHandler or UseSocketsHttpHandler on a guarded client outside tests: either replaces the guard.

Product 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.  net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 0 11/19/2026
1.2.0 417 11/17/2026
1.1.0 4,574 11/5/2026
1.0.0 422 11/5/2026