2017-05-16 57 views
0

我已經閱讀了很多關於如何加密和解密查詢字符串的文章,但似乎無法找到任何關於如何在html標籤中使用它的文章。這是我試圖實現的 產品ID是一個整數,但我不想將它發送到SingleProduct.aspx頁面。我想對它進行加密和解密,然後在網頁上,以使其成爲用於其他操作在asp.net中加密和解密錨標籤查詢字符串

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

查看Mad Kristensen關於HttpModule的關於查詢字符串加密的文章(http://madskristensen.net/post/httpmodule-for-query-string-encryption)。 – kimbaudi

回答

0

我使用用戶的ID(會話[「用戶ID」])作爲我的加密密鑰。 REF 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解密會話[ 「用戶ID」]

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); 
} 

或者,你可以n爲鏈接添加一個GUID字段到詳細id。

+0

感謝您的回覆。有沒有辦法我可以在我的錨html標籤中使用這個? – Mcbaloo

+0

獲取產品數據後如何加密id? – Rainmaker