Maturano
Maturano

Reputation: 1023

Asp.Net MVC encrypted querystring

I am trying to get an encrypted querystring on a ActionResult but the parameter is always null.

I have implemented an e-mail URL and encrypted some informations like this:

string key = "r0b1nr0y";
var queryString = EncryptDecryptQueryString.Encrypt(String.Format("testId={0}&otherInfo={1}", Id, otherInfo), key);

It is resulting in this URL parameter:

?fpZG2mFDOZbuuBFccKLeu9Rzbn/I05i577IaaMSt0uztuHmWdeVIOQ==

And the url is something like this:

www.test.com/MyController/MyAction?fpZG2mFDOZbuuBFccKLeu9Rzbn/I05i577IaaMSt0uztuHmWdeVIOQ==

Now, at the controller side, the ActionResult is suppose to receive a string.

Running the program, when i try to test the generated url, the code reach MyAction but the string parameter is always null.

public ActionResult MyAction(string queryString)
{
     ... Do Stuff here...        
}

I have tried to create a specific route for it as well(like this example), but didn't work.

routes.MapRoute(
            "RouteTest",
            "MyController/MyAction/{queryString}",
            new { controller = "MyController", action = "MyAction", queryString = "" }
        );

I can take the whole url with Request.RawUrl, but i really can't understand why the encrypted string parameter is null(or empty).

Have i missed the point somewhere or is there an explanation for the null parameter value?

Upvotes: 1

Views: 3519

Answers (2)

Mark
Mark

Reputation: 1371

First of all you need to UrlEncode that Base64 string because "/" and "=" have special meaning in your URL.

Second: Without the routing your URL should be: www.test.com/MyController/MyAction?queryString=[UrlEncodedBase64] With a query string parameter that would match the parameter name of your action.

Upvotes: 1

Xavier J
Xavier J

Reputation: 4628

It is null because your method is expecting your url to end with:

?queryString=xxxxxxxx

I understand your approach, but you'll either have to encrypt all the parameters separately, use the approach I've shown above, or write your own model binder for the method that can interpret what's in the URL for your code to consume.

Model binders: https://msdn.microsoft.com/en-us/library/dd410405(v=vs.100).aspx

Upvotes: 3

Related Questions