.NET MVC routing with Url coding issues

I have the following routing code:

routes.MapRoute( "email-validated/{sessionId}", "email-validated/{sessionId}", new { controller = "User", action = "EmailValidated", sessionId = UrlParameter.Optional } ); 

When I find the route to be url encoded, it will not match the route for% 2f,% 2b and some other escaped characters. It will also not match the encoding without url (things w / +, etc.) For example

It works:

E-mail tested / XQiKC 6KMM% 2cmko4nOvzGRwPu9oaZFoCtXsFFJg3ZTf9S5rsBbLGjnz3FN3SJ0apEZcqK1PIcCY28mRMykB39XnFLKaL7390CDfLZi77

This does not work (contains% 2f, etc.):

 email-validated/XQiKC6KMM%2fmko4nOvzGRwPu9oaZFoCtXsFFJg3ZTf9S5rsBbLGjnz3FN3SJ0apEZcqK1PIcCY28mRMykB39XnFLKaL7390CDfLZiV77cso 

This does not work (contains +, etc.)

 email-validated/XQiKC6KMM+mko4nOvzGRwPu9oaZFoCtXsFFJg3ZTf9S5rsBbLGjnz3FN3SJ0apEZcqK1PIcCY28mRMykB39XnFLKaL7390CDfLZiV77cso 
+6
source share
4 answers

It seems that the path of the routing path is escaped / and + is weird. Instead, try passing it as an argument to a query string.

Make the end point:

 routes.MapRoute( "email-validated", "email-validated", new { controller = "User", action = "EmailValidated" } ); 

Call it with a request like:

 email-validated/?sessionId=XQiKC6KMM%2fmko4nOvzGRwPu9oaZFoCtXsFFJg3ZTf9S5rsBbLGjnz3FN3SJ0apEZcqK1PIcCY28mRMykB39XnFLKaL7390CDfLZiV77cso 

And then change your function with

 EmailValidatedFunction(string sessionId) { //...do stuff with sessionId here } 

in

 EmailValidatedFunction() { string sessionId = Request["sessionId"]; //...do stuff with sessionId here } 
+3
source

If possible, you need to make your sessionId URL secure. SessionId is encoded by Base64, and in Base64 there are three characters of the error problem, "/", "+" and "=". To encode your sessionId when creating a link, use the following command:

  public string ToUrlSafeBase64String(string Base64String) { // avoid any slashes, plus signs or equal signs // the following makes this base64 string url safe Base64String = Base64String.Replace("/", "_"); Base64String = Base64String.Replace("+", "-"); return Base64String.Replace("=", String.Empty); } 

Then, to recreate the original Base64 encoded string, use the following:

  public string FromUrlSafeBase64String(string Base64String) { // add back any slashes, plus signs or equal signs // the following makes this url safe string a base64 string Base64String = Base64String.Replace("_", "/"); Base64String = Base64String.Replace( "-", "+"); return Base64String.PadRight(Base64String.Length + (4 - Base64String.Length % 4) % 4, '='); } 
+6
source

Here is the solution I found to allow URL-encoded characters such as slash %2f in ASP.NET MVC URLs.

Add the following to your Application_BeginRequest method of your Global.asax file:

 var realUrl = Request.ServerVariables["HTTP_URL"]; Context.RewritePath(realUrl); 

Since this happens before MVC routing, you can do what you originally wanted to do.

Just remember that you will need to manually decode the parameters in your action methods to get their actual value (since we prevent the creation of this frame).

0
source

you can use System.Web.HttpServerUtility.UrlTokenEncode (from http://brockallen.com/2014/10/17/base64url-encoding/#comments )

0
source

Source: https://habr.com/ru/post/896097/


All Articles