【问题标题】:Correct way to pass base64 info in URL as query string?将 URL 中的 base64 信息作为查询字符串传递的正确方法?
【发布时间】:2019-12-17 07:25:17
【问题描述】:

我通过使用方法btoa() 将它们转换为base64 来发送查询字符串中的一些参数。我只是想知道,我是否还需要将结果包装到encodeURI 方法中以将正确的信息通过URL 传递给服务器。

例如:

  • http://example.com/{name} - 对此,下面哪个是正确的?
  • "http://example.com/" + btoa("some-name")
  • "http://example.com/" + encodeURI(btoa("some-name"))

【问题讨论】:

  • 我的回答能说明你的问题吗?

标签: javascript asp.net asp.net-mvc web server


【解决方案1】:

您只需要将字符串转换为 Base 64。请参阅 this SO answer 了解 C#:

  var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
  return System.Convert.ToBase64String(plainTextBytes);

您也可以阅读the reason why we use Base 64

后面不需要使用encodeURI之类的方法,因为Base 64的所有字符(A-Z,a-z,0-9,=,+)are not escaped

encodeURI 转义所有字符,除了:

A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #

顺便说一句,encodeURIbtoa 是关于 JavaScript,而不是你在标签中提到的 asp.net。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多