Losing the bar ("/")

Asked

Viewed 185 times

1

Follows code:

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 the parameter token, receives bHhETtde1UhKpwUVmTsNTpXZKyfZGK8 and lost the bar.

How do I not lose the "/" ?

Here are some examples of token:

  • pj/Wgche1ujjqg8i/Mqas5zqgp3ob1rn
  • Lk/lpohe1Ujjdftg+Guis5zqgp25b1rn

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

Do not allow special string.

Some solution ?

  • It wouldn’t be the case if you remove the end bar and put it manually getall/{token_string}/ ?

  • The problem is that the token can generate multiple bars.

  • Try to use the Regex.Escape(string)

2 answers

2


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

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

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

And to ensure the integrity of the url, instead of passing as a route parameter, 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 Querystring or route parameter, my understanding is that it should not be exposed so openly. I would use a header attribute for this.

2

Try to use it like this:

var token = @"bHhETtde1UhKpwUVmTsNTpXZKyfZGK8/";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.