Decoding Base64urlUInt-encoded value
RFC 7515 defines base64url encoding like this:
Base64 encoding using the URL- and filename-safe character set defined in Section 5 of RFC 4648, with all trailing '=' characters omitted (as permitted by Section 3.2) and without the inclusion of any line breaks, whitespace, or other additional characters. Note that the base64url encoding of the empty octet sequence is the empty string. (See Appendix C for notes on implementing base64url encoding without padding.)
RFC 4648 defines "Base 64 Encoding with URL and Filename Safe Alphabet" as regular base64, but:
- The padding may be omitted (as it is here)
- Using
-
instead of+
and_
instead of/
So to use regular Convert.FromBase64String
, you just need to reverse that process:
static byte[] FromBase64Url(string base64Url)
{
string padded = base64Url.Length % 4 == 0
? base64Url : base64Url + "====".Substring(base64Url.Length % 4);
string base64 = padded.Replace("_", "/")
.Replace("-", "+");
return Convert.FromBase64String(base64);
}
It's possible that this code already exists somewhere in the framework, but I'm not aware of it.