Soenneker.Security.Parsers.BasicAuth 4.0.25

Prefix Reserved
dotnet add package Soenneker.Security.Parsers.BasicAuth --version 4.0.25
                    
NuGet\Install-Package Soenneker.Security.Parsers.BasicAuth -Version 4.0.25
                    
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="Soenneker.Security.Parsers.BasicAuth" Version="4.0.25" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.Security.Parsers.BasicAuth" Version="4.0.25" />
                    
Directory.Packages.props
<PackageReference Include="Soenneker.Security.Parsers.BasicAuth" />
                    
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 Soenneker.Security.Parsers.BasicAuth --version 4.0.25
                    
#r "nuget: Soenneker.Security.Parsers.BasicAuth, 4.0.25"
                    
#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 Soenneker.Security.Parsers.BasicAuth@4.0.25
                    
#: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=Soenneker.Security.Parsers.BasicAuth&version=4.0.25
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.Security.Parsers.BasicAuth&version=4.0.25
                    
Install as a Cake Tool

alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

Soenneker.Security.Parsers.BasicAuth

A low-allocation parser for HTTP Basic credentials in an ASP.NET Core HttpContext.

Installation

dotnet add package Soenneker.Security.Parsers.BasicAuth

Usage

The returned spans point into a pooled character buffer. Always return that buffer in a finally block after the last credential comparison:

using System.Security.Cryptography;
using System.Text;
using Soenneker.Security.Parsers.BasicAuth;

char[]? credentialBuffer = null;

try
{
    if (!BasicAuthParser.TryReadBasicCredentials(
            httpContext,
            out ReadOnlySpan<char> username,
            out ReadOnlySpan<char> password,
            out credentialBuffer))
    {
        return Results.Unauthorized();
    }

    bool usernameMatches = username.SequenceEqual(configuredUsername);
    byte[] suppliedPassword = Encoding.UTF8.GetBytes(password);

    try
    {
        bool passwordMatches = CryptographicOperations.FixedTimeEquals(
            suppliedPassword,
            configuredPasswordUtf8);

        return usernameMatches && passwordMatches
            ? Results.Ok()
            : Results.Unauthorized();
    }
    finally
    {
        CryptographicOperations.ZeroMemory(suppliedPassword);
    }
}
finally
{
    BasicAuthParser.Clear(credentialBuffer);
}

configuredPasswordUtf8 should be prepared outside the request path and protected like the original secret. In most applications, validate a password through the application's password hasher or identity provider rather than storing a reversible plaintext credential.

Parsing behavior

  • Reads the first Authorization header value and accepts the Basic scheme case-insensitively.
  • Rejects missing or malformed Base64, headers above 8 KiB, and decoded credentials without a non-empty username and password separated by the first colon.
  • Allows additional colons in the password.
  • Returns spans rather than username/password strings, avoiding immutable secret strings during parsing.
  • On success, transfers one rented character buffer to the caller. Call BasicAuthParser.Clear exactly once after the spans are no longer used.
  • On failure, clears and returns any buffer it rented; the returned buffer value is null.

Basic authentication only Base64-encodes credentials; it does not encrypt them. Accept it only over HTTPS, avoid logging the header or decoded values, and apply rate limiting and credential-rotation controls appropriate to the endpoint.

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

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Soenneker.Security.Parsers.BasicAuth:

Package Downloads
Soenneker.Validators.BasicAuth

A lightweight validation module for validating HTTP Basic Authentication credentials.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.25 175 8/30/2026
4.0.24 255 8/29/2026
4.0.23 84 8/29/2026
4.0.22 1,106 7/27/2026
4.0.21 287 7/21/2026
4.0.20 225 7/16/2026
4.0.19 1,000 6/18/2026
4.0.18 827 6/5/2026
4.0.15 1,449 3/12/2026
4.0.14 126 3/12/2026
4.0.12 307 3/12/2026
4.0.11 357 3/10/2026
4.0.10 317 3/9/2026
4.0.9 118 3/9/2026
4.0.8 127 3/9/2026
4.0.7 451 3/4/2026
4.0.6 1,401 1/2/2026
4.0.5 869 11/21/2025
4.0.4 839 10/29/2025
3.0.3 755 9/4/2025
Loading failed

Secure Basic Auth parser buffer handling