简体   繁体   中英

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM