Losing the bar ("/")

1

Follow the code below:

var token = "bHhETtde1UhKpwUVmTsNTpXZKyfZGK8/";
var token_string = Uri.EscapeDataString(token); //"bHhETtde1UhKpwUVmTsNTpXZKyfZGK8%2F"
HttpResponseMessage response = await client.GetAsync($"{URL}/api/getall/{token_string}");

ApiController:

public async Task<IHttpActionResult> GetAll (string token) {}

In parameter token , receive bHhETtde1UhKpwUVmTsNTpXZKyfZGK8 and lost the bar.

How do I not lose " / "?

Here are some examples of a token:

  • pj / Wgche1UjjQG8i / MqaS5ZqGp3Ob1rn
  • lk / lpohe1Ujjdftg + GuiS5ZqGp25b1rn

I use EscapeDataString because of the character + , / , among others.

Do not allow special string.

Any solution?

    
asked by anonymous 19.01.2018 / 02:23

2 answers

2

It loses the last bar because it is interpreted as part of the URL and not as the value of its parameter.

If your token can display special characters it can not be included in a route url in this way (raw) because it will "break" the URL.

The first thing you can do is to use HttpContext.Current.Server.UrlEncode(); or WebUtility.UrlEncode() if we are talking about Asp.Net Core .

And to ensure url integrity, instead of passing as a parameter route, you can use as Query String

HttpResponseMessage response = await client.GetAsync($"{URL}/api/getall/?token={token_string});

But I would not recommend you carry the token as a QueryString or route parameter, my understanding is that it should not be exposed so openly. I would use a header attribute for this.

    
19.01.2018 / 12:06
2

Try using it like this:

var token = @"bHhETtde1UhKpwUVmTsNTpXZKyfZGK8/";
    
22.01.2018 / 13:55