ASP.NET Framework: URL-Safe Base64 Encoding and Decoding
This technique modifies standard Base64 encoding to create URL-friendly strings, eliminating padding and characters like ' ' and '/' that can interfere with URI templates. Here's an ASP.NET implementation:
<code class="language-csharp">/// <summary> /// URL-safe Base64 encoding using UTF-8. /// </summary> /// <param name="str">The string to encode.</param> /// <returns>The URL-safe Base64 encoded string.</returns> public static string UrlSafeBase64Encode(string str) { byte[] encodedBytes = Encoding.UTF8.GetBytes(str); return HttpServerUtility.UrlTokenEncode(encodedBytes); } /// <summary> /// URL-safe Base64 decoding using UTF-8. /// </summary> /// <param name="str">The URL-safe Base64 encoded string.</param> /// <returns>The decoded string.</returns> public static string UrlSafeBase64Decode(string str) { byte[] decodedBytes = HttpServerUtility.UrlTokenDecode(str); return Encoding.UTF8.GetString(decodedBytes); }</code>
To utilize this URL-safe Base64 encoding, simply replace your existing Base64 encoding and decoding methods with the functions provided above.
Important Considerations:
The above is the detailed content of How to Implement Modified Base64 URL Encoding and Decoding in ASP.NET?. For more information, please follow other related articles on the PHP Chinese website!