ASP.NET MVC - 在URL加密数据路由 [英] ASP.NET MVC - Encrypt Route Data in URL

查看:693
本文介绍了ASP.NET MVC - 在URL加密数据路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET MVC应用程序,我想的路线数据和不加密查询字符串,在其他字:

In my ASP.NET MVC app, I want to encrypt the route data and NOT QueryString, in other word:

我使用的是ASP.NET MVC默认路由模式:

I'm using the ASP.NET MVC default route pattern :

   routes.MapRoute(
             name: "Default",
            url: "{controller}/{action}/{id}",
             defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
             namespaces: new[] { "WHATEVER" }
         ); 

和我有行动的方法采取ID参数:

And I have Action Method take id Parameter:

  public ActionResult Example(int id)
  {
    return View();
  }

所以我的网址我们将数据传递给这个行动方法是:

So My Url Now to pass the data to this Action Method is:

/控制器/例/ 10

和我想它像这样

/控制器/示例/ ENCRYPTEDPARAMTER

由于提前

推荐答案

我发现的不错指南做我想做什么,但与查询字符串的但不要将数据,所以我更新它用查询字符串和路由数据的工作,该指南是基于创建自定义助手和自定义属性,所以很容易把我的变化,我将code我的变化提及:

I found a nice guide to do what I want but with QueryString BUT NOT Route Data, So I updated it to work with both QueryString and Route Data, the guide was based on creating custom Helper and custom Attribute, so put my changes easily, I will mention by code my changes:

自定义助手

  public static MvcHtmlString EncodedActionLink(this HtmlHelper htmlHelper, string linkText, string Action, string ControllerName, string Area, object routeValues, object htmlAttributes)
    {
        string queryString = string.Empty;
        string htmlAttributesString = string.Empty;
       //My Changes
        bool IsRoute = false;
        if (routeValues != null)
        {
            RouteValueDictionary d = new RouteValueDictionary(routeValues);
            for (int i = 0; i < d.Keys.Count; i++)
            {
                 //My Changes
                if (!d.Keys.Contains("IsRoute"))
                {
                    if (i > 0)
                    {
                        queryString += "?";
                    }
                    queryString += d.Keys.ElementAt(i) + "=" + d.Values.ElementAt(i);
                }
                else
                {
                    //My Changes
                    if (!d.Keys.ElementAt(i).Contains("IsRoute"))
                    {
                        queryString += d.Values.ElementAt(i);
                        IsRoute = true;
                    }
                }
            }
        }

        if (htmlAttributes != null)
        {
            RouteValueDictionary d = new RouteValueDictionary(htmlAttributes);
            for (int i = 0; i < d.Keys.Count; i++)
            {
                htmlAttributesString += " " + d.Keys.ElementAt(i) + "=" + d.Values.ElementAt(i);
            }
        }


        StringBuilder ancor = new StringBuilder();
        ancor.Append("<a ");
        if (htmlAttributesString != string.Empty)
        {
            ancor.Append(htmlAttributesString);
        }
        ancor.Append(" href='");
        if (Area != string.Empty)
        {
            ancor.Append("/" + Area);
        }

        if (ControllerName != string.Empty)
        {
            ancor.Append("/" + ControllerName);
        }

        if (Action != "Index")
        {
            ancor.Append("/" + Action);
        }
        //My Changes
        if (queryString != string.Empty)
        {
            if (IsRoute == false)
                ancor.Append("?q=" + Encrypt(queryString));
            else
                ancor.Append("/" + Encrypt(queryString));
        }
        ancor.Append("'");
        ancor.Append(">");
        ancor.Append(linkText);
        ancor.Append("</a>");
        return new MvcHtmlString(ancor.ToString());
    }

自定义属性:

  public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        int Id;

        Dictionary<string, object> decryptedParameters = new Dictionary<string, object>();
        if (HttpContext.Current.Request.QueryString.Get("q") != null)
        {
            string encryptedQueryString = HttpContext.Current.Request.QueryString.Get("q");
            //string decrptedString = Decrypt(encryptedQueryString.ToString());
            string decrptedString = Decrypt(encryptedQueryString.ToString());
            string[] paramsArrs = decrptedString.Split('?');

            for (int i = 0; i < paramsArrs.Length; i++)
            {
                string[] paramArr = paramsArrs[i].Split('=');
                decryptedParameters.Add(paramArr[0], Convert.ToInt32(paramArr[1]));
            }
        }
        else if (!HttpContext.Current.Request.Url.ToString().Contains("?"))
        {
            //if (int.TryParse(Decrypt(HttpContext.Current.Request.Url.ToString().Split('/').Last()), out Id))
            if (int.TryParse(Decrypt(HttpContext.Current.Request.Url.ToString().Split('/').Last()), out Id))
            {
                string id = Id.ToString();
                decryptedParameters.Add("id", id);
            }
        }
        else if (HttpContext.Current.Request.Url.ToString().Contains("?"))
        {
            //if (int.TryParse(Decrypt(HttpContext.Current.Request.Url.ToString().Split('/').Last().Split('?')[0]), out Id))
            if (int.TryParse(Decrypt(HttpContext.Current.Request.Url.ToString().Split('/').Last().Split('?')[0]), out Id))
            {
                string id = Id.ToString();
                if (id.Contains('?'))
                {
                    id = id.Split('?')[0];
                }
                decryptedParameters.Add("id", id);
            }

            string[] paramsArrs = HttpContext.Current.Request.Url.ToString().Split('/').Last().Split('?');
            for (int i = 1; i < paramsArrs.Length; i++)
            {
                string[] paramArr = paramsArrs[i].Split('=');
                decryptedParameters.Add(paramArr[0], Convert.ToInt32(paramArr[1]));
            }
        }

        for (int i = 0; i < decryptedParameters.Count; i++)
        {
            filterContext.ActionParameters[decryptedParameters.Keys.ElementAt(i)] = decryptedParameters.Values.ElementAt(i);
        }
        base.OnActionExecuting(filterContext);

    }

其实我的变化不是那么大,所以实际空间日志相册为此的指南

这篇关于ASP.NET MVC - 在URL加密数据路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆