I am trying to remove the dependency on System.Web.dll from a web API project, but stumbled upon a call to HttpServerUtility.UrlTokenEncode(byte[] input) (and its corresponding decoding method), which I do not know what to replace in order to provide backward compatibility. The documentation states that this method
Encodes an array of bytes into its equivalent string representation, using base 64 digits that can be used to pass to a URL.
I tried replacing Convert.ToBase64String(byte[] input) (and its corresponding decoding method), which is very similarly described in the docs:
Converts an array of 8-bit unsigned integers to its equivalent string representation, which is encoded with base-64 digits.
However, they do not seem completely equivalent; when using Convert.FromBase64String(string input) to decode a string encoded with HttpServerUtility , I get an exception indicating
The input is not a valid Base-64 string because it contains a non-base 64 character, more than two padding characters, or an invalid character among padding characters.
What is the difference between these two conversion utilities? What is the correct way to remove this dependency on System.Web.HttpServerUtility ?
Some users have suggested that this is a duplicate of this , but I disagree. This question is related to base-64-encoded string in the form of url-safe in general, but I need to reproduce the exact behavior of HttpServerUtility , but without dependence on System.Web .
source share