0

I am migrating from .net framework to .net core. In my current application I have:

private static string ToBase64Url(string value) => ToBase64Url(Encoding.UTF8.GetBytes(value));

private static string ToBase64Url(byte[] value)
{
  var s = HttpServerUtility.UrlTokenEncode(value).ToCharArray();
  return new string(s, 0, s.Length - 1);
}

I tried changing that to:

private static string ToBase64Url(byte[] value)
{
  var s = HttpUtility.UrlEncode(value).ToCharArray();
  return new string(s, 0, s.Length - 1);
}

However that gives me different results:

test data:

var text = {"alg":"RS256","typ":"JWT"};

result in .net framework: eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9

result in .net core: %7b%22alg%22%3a%22RS256%22%2c%22typ%22%3a%22JWT%22%7

what should be the right equivalent here?

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
user122222
  • 2,179
  • 4
  • 35
  • 78
  • 2
    Hope this helps: https://stackoverflow.com/questions/50731397/httpserverutility-urltokenencode-replacement-for-netstandard – abhinav pratap Jul 21 '21 at 17:28
  • 2
    Does this answer your question? [HttpServerUtility.UrlTokenEncode replacement for netstandard](https://stackoverflow.com/questions/50731397/httpserverutility-urltokenencode-replacement-for-netstandard) – Ian Kemp Jul 21 '21 at 19:34
  • yes, thank you alot – user122222 Jul 21 '21 at 20:13

1 Answers1

0

Using the link above I created shorter version that worked for me:

private static string ToBase64Url(byte[] value) =>
            Convert.ToBase64String(value).Replace('+', '-').Replace('/', '_').TrimEnd('=');
user122222
  • 2,179
  • 4
  • 35
  • 78