Base64Url Class
Polyfill of the BCL Base64Url shipping in net9.0+, providing strict RFC 7515 §3
(and RFC 4648 §5) base64url decoding/encoding without padding for the net8.0 target. The
shape mirrors the BCL surface so consumer code references System.Buffers.Text.Base64Url
uniformly across all TFMs we ship to. Removed in the same commit that drops the net8.0 target
after net8.0 reaches end-of-life on 2026-11-10.
public static class Base64UrlInheritance System.Object → Base64Url
Methods
Base64Url.DecodeFromChars(ReadOnlySpan<char>) Method
Decodes a base64url-encoded character span into a byte array. Strict per RFC 7515 §3:
rejects characters outside the alphabet A-Z a-z 0-9 - _, rejects standard-base64
alphabet characters + and /, rejects padding =, and rejects inputs
whose length leaves length mod 4 == 1.
public static byte[] DecodeFromChars(System.ReadOnlySpan<char> source);Parameters
source System.ReadOnlySpan<System.Char>
The base64url-encoded characters.
Returns
System.Byte[]
The decoded bytes; an empty array when source is empty.
Exceptions
System.FormatException
Thrown when source contains a
character outside the base64url alphabet, or when its length leaves a 1-character
remainder modulo 4.
Base64Url.EncodeToString(ReadOnlySpan<byte>) Method
Encodes a byte span as a base64url string per RFC 7515 §3, without trailing padding.
Replaces standard-base64 + with - and / with _.
public static string EncodeToString(System.ReadOnlySpan<byte> source);Parameters
source System.ReadOnlySpan<System.Byte>
The bytes to encode.
Returns
System.String
The base64url-encoded string; System.String.Empty when
source is empty.
Base64Url.TryDecodeFromChars(ReadOnlySpan<char>, Span<byte>, int) Method
Decodes a base64url-encoded character span into destination.
Mirrors the .NET 9+ BCL surface so consumer code can use a single call shape
across TFMs. Returns false when destination is too
small to hold the decoded bytes; throws System.FormatException on
invalid characters or length-mod-4-of-1 inputs, matching BCL semantics -
the "Try" prefix here covers buffer-size failures, not malformed input.
public static bool TryDecodeFromChars(System.ReadOnlySpan<char> source, System.Span<byte> destination, out int bytesWritten);Parameters
source System.ReadOnlySpan<System.Char>
The base64url-encoded characters.
destination System.Span<System.Byte>
Buffer to receive the decoded bytes.
bytesWritten System.Int32
On success, the number of bytes written into
destination; otherwise 0.
Returns
System.Boolean
true on successful decode; false when the destination
is too small.
Exceptions
System.FormatException
Thrown when source contains
a character outside the base64url alphabet, or when its length leaves a
1-character remainder modulo 4.