Base64Url Class
Definition
Provides the RFC 4648 §5 URL- and filename-safe Base64 encoding as a first-class type, mirroring the shape of
System.Buffers.Text.Base64Url introduced in .NET 9. URL-safe Base64 swaps + and / for -
and _, and conventionally omits trailing = padding (matching JWT and OAuth practice).
public static class Base64Url
- Inheritance
-
Base64Url
- Inherited Members
Examples
// Encode a JWT segment - URL-safe alphabet, no padding.
byte[] header = "{\"alg\":\"HS256\",\"typ\":\"JWT\"}"u8.ToArray();
string segment = Base64Url.Encode(header); // "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"
// Decoder accepts inputs both with and without trailing '=' padding.
byte[] roundtrip = Base64Url.Decode(segment);
byte[] padded = Base64Url.Decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9==");
Remarks
This is a convenience wrapper over Base64 with UrlSafe pre-bound. Code that knows it wants URL-safe Base64 should prefer this type over passing the variant explicitly to every call. Both forms produce identical output.
Methods
Decode(ReadOnlySpan<byte>)
Decodes UTF-8 URL-safe Base64 bytes into a byte array.
public static byte[] Decode(ReadOnlySpan<byte> utf8Source)
Parameters
utf8SourceReadOnlySpan<byte>The UTF-8 URL-safe Base64 source.
Returns
- byte[]
The decoded byte array.
Exceptions
- FormatException
Thrown when the input is not valid URL-safe Base64.
Decode(ReadOnlySpan<char>)
Decodes chars as a URL-safe Base64 character span.
public static byte[] Decode(ReadOnlySpan<char> chars)
Parameters
charsReadOnlySpan<char>The URL-safe Base64 character span.
Returns
- byte[]
The decoded byte array.
Exceptions
- FormatException
Thrown when the input is not valid URL-safe Base64.
Decode(string)
Decodes s as a URL-safe Base64 string. Accepts inputs both with and without trailing
= padding (the JWT / OAuth convention is to omit it).
public static byte[] Decode(string s)
Parameters
sstringThe URL-safe Base64 input.
Returns
- byte[]
The decoded byte array.
Exceptions
- ArgumentNullException
Thrown when
sis null.- FormatException
Thrown when the input is not valid URL-safe Base64.
Encode(byte[])
Encodes bytes into a URL-safe Base64 string without padding.
public static string Encode(byte[] bytes)
Parameters
bytesbyte[]The bytes to encode.
Returns
- string
The URL-safe Base64 string.
Exceptions
- ArgumentNullException
Thrown when
bytesis null.
Encode(ReadOnlySpan<byte>)
Encodes bytes into a URL-safe Base64 string without padding.
public static string Encode(ReadOnlySpan<byte> bytes)
Parameters
bytesReadOnlySpan<byte>The bytes to encode.
Returns
- string
The URL-safe Base64 string.
EncodeToUtf8(ReadOnlySpan<byte>)
Encodes source into a UTF-8 byte array using the URL-safe Base64 alphabet.
public static byte[] EncodeToUtf8(ReadOnlySpan<byte> source)
Parameters
sourceReadOnlySpan<byte>The bytes to encode.
Returns
- byte[]
The UTF-8 encoded URL-safe Base64 bytes.
GetEncodedLength(int)
Returns the maximum number of characters required to encode byteCount bytes in URL-safe
Base64 (without padding by default).
public static int GetEncodedLength(int byteCount)
Parameters
byteCountintThe input byte count.
Returns
- int
The encoded character count.
GetMaxDecodedLength(int)
Returns the maximum number of bytes that decoding charCount characters can produce.
public static int GetMaxDecodedLength(int charCount)
Parameters
charCountintThe input character count.
Returns
- int
The maximum decoded byte count.
IsValid(ReadOnlySpan<char>)
Indicates whether source is a valid URL-safe Base64 input.
public static bool IsValid(ReadOnlySpan<char> source)
Parameters
sourceReadOnlySpan<char>The input characters.
Returns
TryDecode(ReadOnlySpan<char>, Span<byte>, out int)
Attempts to decode chars as URL-safe Base64 into destination.
public static bool TryDecode(ReadOnlySpan<char> chars, Span<byte> destination, out int bytesWritten)
Parameters
charsReadOnlySpan<char>The URL-safe Base64 character span.
destinationSpan<byte>The destination byte span.
bytesWrittenintWhen this method returns, contains the number of bytes written.
Returns
TryEncode(ReadOnlySpan<byte>, Span<char>, out int)
Attempts to encode source into destination as URL-safe Base64.
public static bool TryEncode(ReadOnlySpan<byte> source, Span<char> destination, out int charsWritten)
Parameters
sourceReadOnlySpan<byte>The bytes to encode.
destinationSpan<char>The destination span.
charsWrittenintWhen this method returns, contains the number of characters written.
Returns
TryEncodeToUtf8(ReadOnlySpan<byte>, Span<byte>, out int)
Attempts to encode source into destination as URL-safe Base64 UTF-8
bytes.
public static bool TryEncodeToUtf8(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)
Parameters
sourceReadOnlySpan<byte>The bytes to encode.
destinationSpan<byte>The destination UTF-8 byte span.
bytesWrittenintWhen this method returns, contains the number of bytes written.
Returns
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |