【问题标题】:How can I encrypt the id querystring parameter如何加密 id 查询字符串参数
【发布时间】:2019-06-25 14:17:50
【问题描述】:

我需要发送带有加密的“@id”参数(在浏览器搜索栏上)然后解密 id 以从编辑视图中获取 id 的值。如何使用 AES 加密。

public ActionResult Edit(int? id)
{
    return view(model);
}

【问题讨论】:

    标签: c# asp.net-mvc cryptography


    【解决方案1】:

    您无法加密和解密 URL 中查询字符串参数中的参数。如果要在客户端浏览器和主机系统之间传递参数,则必须将参数隐藏在主机和客户端之间的消息传递内容中,而整个消息传递通过 HTTPS 加密。

    【讨论】:

      【解决方案2】:

      您可以通过以下步骤完成:

      第 1 步: 在您的项目中创建一个新类并从This Link 复制粘贴代码。

      第 2 步: 立即构建项目

      第 3 步:

      MyExtension 命名空间放在页面顶部(视图)

      @Html.EncodedActionLink(item.Name, "YourActionName", "YourControllerName", new { id = item.ID }, null)
      

      第 4 步:

      [EncryptedActionParameter]
      public ActionResult Edit(int? id)
      {
          return view(model);
      }
      

      已编辑: 完成上述步骤后,发现解密有问题,我将其更改。因此您需要将DecryptMyExtensions 类中的DecryptEncrypt 方法中的byte[] IV = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 }; 更改为byte[] IV = { 55, 34, 87, 64, 87, 195, 54, 21 };

      【讨论】:

      • 我添加了这些类,并且 [EncryptedActionParameter] public ActionResult Edit(int? id) { return view(model); } 但它说“Html 助手不包含 EncodedActionLink 扩展方法”
      • 添加这个using System.Web.Mvc.Html;
      • 在哪里,我添加了它,但 VStudio 说它没有使用。我正在使用 Devexpress,它发生在 gridviewpartial
      • 现在 url 是 :"/Edit?q=+kBlw5MHdHA= " 但我使用的是 Edit/2,它没有在帖子页面上解密
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      • 2020-11-09
      • 1970-01-01
      相关资源
      最近更新 更多