【问题标题】:Encrypting and Decrypting anchor tag query string in asp.net加密和解密asp.net中的锚标记查询字符串
【发布时间】:2017-05-16 08:01:50
【问题描述】:

我已经阅读了很多关于如何加密和解密查询字符串的文章,但似乎找不到任何关于如何在 html 标记中使用它的文章。这就是我想要实现的 产品 id 是一个整数,但我不想像那样将它发送到 SingleProduct.aspx 页面。我想加密它然后在页面上解密它以使其用于其他操作

<a href="Singleproduct.aspx?Product=<%#Eval("Product_Id")) %>">

【问题讨论】:

标签: asp.net query-string


【解决方案1】:

我使用用户 ID (Session["UserId"]) 作为我的加密密钥。参考RijndaelManaged Class
productlist.aspx 喜欢

<a href="Singleproduct.aspx?enProduct=<%#EncodeId(Eval("id")) %>">En Product Detail</a>


productlist.aspx.cs 喜欢

protected void Page_Load(object sender, EventArgs e)
{
    Session["UserId"] = "rainmaker";
}

protected string EncodeId(object id)
{
    var encryptKey = (string)Session["UserId"];
    var encryptKeyArray = Encoding.ASCII.GetBytes(encryptKey);
    Array.Resize(ref encryptKeyArray, 16);
    // Encrypt the string to an array of bytes.
    byte[] encrypted = EncryptStringToBytes(Convert.ToString(id), encryptKeyArray, encryptKeyArray);
    string encryptedStr = Convert.ToBase64String(encrypted).Replace('+', '-').Replace('/', '_');
    return encryptedStr;
}


Singleproduct.aspx.cs 按会话解密["UserId"]

var enProductId = Request.QueryString["enProduct"];
if (enProductId != null)
{
    var encryptKey = (string)Session["UserId"];
    var encryptKeyArray = Encoding.ASCII.GetBytes(encryptKey);
    Array.Resize(ref encryptKeyArray, 16);
    var encryptedArray = Convert.FromBase64String(enProductId.Replace('_', '/').Replace('-', '+')); 
    // Decrypt the bytes to a string.
    string id = DecryptStringFromBytes(encryptedArray, encryptKeyArray, encryptKeyArray);
    Response.Write(id);
}

或者,您可以添加一个 GUID 字段以链接到详细信息 ID。

【讨论】:

  • 感谢您的回复。有没有办法我可以在我的锚 html 标签中使用它?
  • 获取商品数据后加密id怎么样?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-21
  • 2010-12-02
  • 2012-07-01
相关资源
最近更新 更多