我使用用户 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。