开发者

How to encrypt URL parameters in MVC

I'm trying to encrypt the URL parameters by implementing an EncryptedActionLink that returns a link with an encrypted pa开发者_如何学Gorameter "p" to a generic action "ResolveUrl". The controller should recieve the request and invoke the proper action, or redirect it to the actual action without showing later the unencrypted values at the address bar (RedirectToAction doesn't work because of this).

So far, I've done this extension method:

    public static MvcHtmlString EncryptedActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
    {
        var RouteValueDictionary = new RouteValueDictionary(routeValues);

        RouteValueDictionary.Add("actionName", actionName);
        RouteValueDictionary.Add("noise", new Random().Next(5000,10000));

        var routeValuesText = RouteTable.Routes.GetVirtualPath(null, RouteValueDictionary).VirtualPath;            
        var Encryption64 = new Encryption64();
        var routeValuesTextCrypto = Encryption64.Encrypt(routeValuesText, "ABC123AB");

        return htmlHelper.ActionLink(linkText, "ResolveUrl", controllerName, new { p = routeValuesTextCrypto }, htmlAttributes);
    }

using this method, i get the following URL:

<%: Html.EncryptedActionLink("MyText", "MyAction", "MyContoller", new { Parameter1 = 123, Parameter2 = "String", Parameter3 = false }, null)%>

http://localhost:21536/MyContoller/ResolveUrl?p=iqo6yhy0Zl3jZXdMmnJ9KdvQhqCb5X6gg19%2FqZ8XUe19r5PJ6xO84plZr1GUHCHNY9h2SDO1o4CaF9W2DdmpywXooEQ1S0rNYjpnH4s3wb%2FqM8sGxoqAqyIoC%2F2nqW7U

Now, all my contollers inherits from ContollerBase. There I define the ResolveUrl Action as this:

public ActionResult ResolveUrl(String p)
        {
            var Encryption64 = new Encryption64();
            var query = Encryption64.Decrypt(p, "ABC123AB");

            if (query.Length > 2)
                query = query.Substring(2);

            var tokens = query.Split(new String [] { "&" }, StringSplitOptions.RemoveEmptyEntries);
            var RouteValueDictionary = new RouteValueDictionary();

            for (int i = 0; i < tokens.Count(); i++)
            {
                var centerPos = tokens[i].IndexOf("=");
                RouteValueDictionary.Add(tokens[i].Substring(0,centerPos),tokens[i].Substring(centerPos+1));
            }

            Type thisType = this.GetType();
            MethodInfo theMethod = thisType.GetMethod(RouteValueDictionary["actionName"].ToString());
            var theParameters = theMethod.GetParameters();
            var theParametersObject = new object[theParameters.Count()];

            System.ComponentModel.TypeConverter converter = new System.ComponentModel.TypeConverter();

            for (int i=0 ; i<theParameters.Count();i++)
            {
                theParametersObject[i] = converter.ConvertTo(RouteValueDictionary[theParameters[i].Name],theParameters[i].ParameterType);
            }

            return (ActionResult)theMethod.Invoke(this, theParametersObject);
        }

the thing about that code is that the ResolveUrl doesn't work. First, when there are two implementatios for one action (POST/GET) then an exception is throwed. And the second thing that fails is the parameter type conversion (for exampte converting from string to an nullable type).

How can I encrypt the URL parameters? Is any of my code useful? What is the best way to do this?


if you are trying to encrypt url parameters (route values) you can use custom valuedataprovider that will automatically decrypt the value on action without showing unencrypted value in address bar.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜