Skip to main content

How to Base64url-Encode a String in C# with Base64UrlEncoder

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Base64url is base64 made safe for web addresses: - and _ instead of + and /, and no = padding. JSON Web Tokens use it for every piece. In .NET, the Base64UrlEncoder class from the Microsoft.IdentityModel.Tokens package encodes and decodes it in one call.

Syntax​

Syntax
using Microsoft.IdentityModel.Tokens;

string <encoded> = Base64UrlEncoder.Encode(<text>);
string <text> = Base64UrlEncoder.Decode(<encoded>);
PartWhat it does
Base64UrlEncoder.Encode(<text>)Encodes the string as UTF-8 and returns base64url without padding.
Base64UrlEncoder.Decode(<encoded>)Decodes base64url text back into a UTF-8 string.

Microsoft Learn on Base64UrlEncoder.Encode(String): it "Performs base64url encoding, which differs from regular base64 encoding as follows: Padding is skipped so the pad character '=' doesn't have to be percent encoded. The 62nd and 63rd regular base64 encoding characters ('+' and '/') are replaced with ('-' and '_'). This makes the encoding alphabet URL safe."

The Example​

The example encodes the text Hi?> once with plain base64, Convert.ToBase64String, and once with Base64UrlEncoder.Encode, then decodes the base64url result back into text. Those four characters were chosen because their base64 contains a / and padding.

Prerequisites​

  • .NET SDK. This guide uses .NET SDK 10.0.404.

Step 1: Create the Project​

Terminal
dotnet new console -o demo
cd demo
dotnet package add Microsoft.IdentityModel.Tokens

dotnet new console creates a console app in a new demo folder, and dotnet package add adds the Microsoft.IdentityModel.Tokens package, version 8.23.0 here, which holds Base64UrlEncoder.

Step 2: Write the Code​

Replace the contents of Program.cs:

Program.cs, which encodes Hi?> with Convert.ToBase64String of its UTF-8 bytes, encodes it with Base64UrlEncoder.Encode, and decodes SGk_Pg with Base64UrlEncoder.Decode.

Program.cs
using System.Text;
using Microsoft.IdentityModel.Tokens;

var text = "Hi?>";

Console.WriteLine(Convert.ToBase64String(Encoding.UTF8.GetBytes(text)));
Console.WriteLine(Base64UrlEncoder.Encode(text));
Console.WriteLine(Base64UrlEncoder.Decode("SGk_Pg"));

Step 3: Run It​

Terminal
dotnet run
Output
SGk/Pg==
SGk_Pg
Hi?>

A zsh terminal where dotnet run prints SGk/Pg== for base64, SGk_Pg for base64url, and Hi?> after decoding.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • Convert.ToBase64String gave SGk/Pg==: a / and two = of padding.
  • Base64UrlEncoder.Encode gave SGk_Pg: the / became _, and the padding is gone.
  • Base64UrlEncoder.Decode turned SGk_Pg back into Hi?>.

Common Mistakes​

MistakeWhat happensDo this instead
Using Convert.ToBase64String where base64url is requiredThe text can contain +, / and =Use Base64UrlEncoder.Encode
Decoding binary data with Decode, which returns a stringBytes that aren't UTF-8 text come out garbledUse Base64UrlEncoder.DecodeBytes
Adding the package only for thisIt works, but it's an extra dependencyWithout the package, .NET 10's System.Buffers.Text.Base64Url.EncodeToString(bytes) gave the same SGk_Pg

Troubleshooting​

SymptomLikely causeFix
error CS0103: The name 'Base64UrlEncoder' does not exist in the current contextThe using Microsoft.IdentityModel.Tokens; line or the package is missingAdd both
The output ends in =The text was encoded with Convert.ToBase64StringUse Base64UrlEncoder.Encode

FAQ​

Can Decode read plain base64?​

Yes. Base64UrlEncoder.Decode("SGk/Pg==") also gave Hi?>.

Is this the same encoding a JWT uses?​

Yes. JsonWebTokenHandler uses base64url for the header, the payload and the signature of every token it writes.

Conclusion​

Base64UrlEncoder.Encode(text) gives the unpadded, URL-safe base64 that JWTs use, and Base64UrlEncoder.Decode(encoded) turns it back into text. Both come with the Microsoft.IdentityModel.Tokens package.

Additional Resources​