10

I am using HttpUtility.UrlEncode() on a string token the original token is t+Bj/YpH6zE= when i HttpUtility.UrlDecode() it becomes t Bj/YpH6zE= which breaks the algorithm. is a way to stop changing + to a space in c#.

I am currently using replace method to achieve that var token_decrypt = HttpUtility.UrlDecode(token).Replace(" ", "+");

public HttpResponseMessage RegisterUser(User user, string token)
        {
            int accID;

            if(string.IsNullOrWhiteSpace(token))
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
            else
            {
                try
                {
                     // token now = t Bj/YpH6zE= which will fail 
                     var token_decrypt = HttpUtility.UrlDecode(token);
                      token now = t Bj/YpH6zE= still the same 
                     accID = int.Parse(Crypto.Decrypt(token_decrypt, passPhrase));
                }
                catch
                {
                    return Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid account ");
                }

her i encode the token

  string encoded_token = HttpUtility.UrlEncode(token);

            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            msg.To.Add(mail);
            msg.From = new System.Net.Mail.MailAddress(from);

her is the call of RegisterUser from angular js

 RegistrationFactory.registerUser = function(user, token){
    return $http({method : "post", url:ConfigService.baseUrl+"User/RegisterUser?token="+token, data : user});
};
MohamedAbbas
  • 1,149
  • 4
  • 15
  • 31

2 Answers2

7

No need to stop changing + to . If you use UrlEncode and UrlDecode correctly. Below code works as expected

var newtoken = HttpUtility.UrlEncode("t+Bj/YpH6zE=");
//newtoken is t%2bBj%2fYpH6zE%3d now
var orgtoken = HttpUtility.UrlDecode(newtoken);
//orgtoken: t+Bj/YpH6zE=

and for bonus

byte[] buf = Convert.FromBase64String(orgtoken);
L.B
  • 114,136
  • 19
  • 178
  • 224
  • 1
    orgtoken is = to t Bj/YpH6zE= which breaks my decoding algorithm i need it to still have + – MohamedAbbas Aug 04 '14 at 12:16
  • @user2918388 No, Just copy above code and try it... Don't pass your original token to `UrlDecode`, give it the encoded one. – L.B Aug 04 '14 at 12:16
  • that's what i do i will check again to make sure. – MohamedAbbas Aug 04 '14 at 12:18
  • I have tested it agien and what i get first token is t+Bj/YpH6zE= then t%2BBj%2FYpH6zE%3D i decode ot it become t Bj/YpH6zE= – MohamedAbbas Aug 04 '14 at 12:28
  • 1
    @user2918388 I don't see your code but what you say is imposible. You must be doing double-decode somewhere. See above code. I tested it and it works. Even the base64 conversion. Or better, post a [sscce](http://sscce.org/) so that we can talk on it. – L.B Aug 04 '14 at 12:32
  • @user2918388 You are doing something horribly wrong if this isn't working for you. Please show us the EXACT code you have to achieve this. – DavidG Aug 04 '14 at 12:42
  • Wrong Input. Invalid length for a Base-64 char array or string. That what i get when using var token_decrypt = HttpUtility.UrlDecode(token) – MohamedAbbas Aug 04 '14 at 12:46
  • HttpUtility.UrlDecode(token).Replace(" ", "+") that one works ! – MohamedAbbas Aug 04 '14 at 12:47
  • @user2918388 Have you read my comment about [sscce](http://sscce.org/) ? How hard can it be to prepare a short, compilable example. – L.B Aug 04 '14 at 12:48
  • @user2918388 How do you think I can reproduce your case. By writing a complete web application? Prepare a small Console application. You can even use Linqpad... – L.B Aug 04 '14 at 13:06
  • 1
    If I have to guess a MVC controller is used, which will already decode the querystring parameter – Yeronimo Sep 11 '18 at 13:44
  • @L.B - I have a similar problem. Look at this string `A+A`. If you use `HttpUtility.UrlDecode("A+A")` it will produce `A A`. I do not have control over this. It passes to me from a 3rd party WebServer. I complained to their support, but they said we should use the standard Url Decode function. If you use https://www.urldecoder.org/ you will see that `A+A` will be decoded to `A+A`. The question is, how to make HttpUtility.UrlDecode to behave like a standard decoder? – Sam Oct 14 '22 at 11:36
5

You can use the UrlPathEncode method, the documentation of UrlEncode method mentions the following

You can encode a URL using with the UrlEncode method or the UrlPathEncode method. However, the methods return different results. The UrlEncode method converts each space character to a plus character (+). The UrlPathEncode method converts each space character into the string "%20", which represents a space in hexadecimal notation. Use the UrlPathEncode method when you encode the path portion of a URL in order to guarantee a consistent decoded URL, regardless of which platform or browser performs the decoding.

More details are available at http://msdn.microsoft.com/en-us/library/4fkewx0t(v=vs.110).aspx

codelion
  • 1,056
  • 1
  • 11
  • 17