简体   繁体   中英

HttpUtility.UrlEncode in .net core

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?

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

private static string ToBase64Url(byte[] value) =>
            Convert.ToBase64String(value).Replace('+', '-').Replace('/', '_').TrimEnd('=');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM