What is the difference between Convert.ToBase64String(byte[]) and HttpServerUtility.UrlTokenEncode(byte[])?

后端 未结 2 2000
刺人心
刺人心 2021-02-20 08:00

I\'m trying to remove a dependence on System.Web.dll from a Web API project, but have stumbled on a call to HttpServerUtility.UrlTokenEncode(byte[] input) (and its

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-20 08:38

    HttpServerUtility.UrlTokenEncode(byte[] input) will encode a URL safe Base64 string. In Base64 +, / and = characters are valid, but they are not URL safe, this method will replace these characters whereas the Convert.ToBase64String(byte[] input) will not. You can probably drop the reference and do it yourself.

    Usually, '+' is replaced with '-' and '/' with '_' padding '=' is just removed.

    Accepted answer here gives a code example: How to achieve Base64 URL safe encoding in C#?

提交回复
热议问题