简体   繁体   English

.net 核心中的 HttpUtility.UrlEncode

[英]HttpUtility.UrlEncode in .net core

I am migrating from .net framework to .net core.我正在从 .net 框架迁移到 .net 核心。 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 .net 框架中的结果: eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9

result in .net core: %7b%22alg%22%3a%22RS256%22%2c%22typ%22%3a%22JWT%22%7 .net 核心的结果: %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('=');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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