2010-07-13 93 views
11

在我的一個ASP.Net網站中,我必須提供一個鏈接,指向所有查詢字符串參數應該加密的用戶。如何在ASP.NET網站中加密查詢字符串參數?

我在想什麼是使用命令"aspnet_regiis"(用於加密web.config數據),將輸出作爲查詢字符串傳遞到發佈的url中。

當用戶單擊該鏈接時,我首先解密該字符串,然後獲取查詢字符串的原始數據。

我對嗎?有沒有什麼好的技術來加密和解密查詢字符串?

+0

您不需要調用外部應用程序。使用框架內的加密API來加密/解密數據。 http://msdn.microsoft.com/en-us/library/system.security.cryptography(VS.71).aspx – onof 2010-07-13 10:14:27

+0

下面是我如何處理此任務:http://blackbeltcoder.com/Articles/asp/encrypting-查詢參數。 – 2010-12-19 16:48:26

+0

這是所有內置於ASP.NET的 - 無需編寫您自己的加密:http://brockallen.com/2012/06/21/use-the-machinekey-api-to-protect-values-in-asp -淨/ – 2012-11-22 15:37:29

回答

5

在ASP.NET上下文加密和解密字符串的一個好方法是使用FormsAuthentication.Encrypt Method

這似乎是隻適合於餅乾,但它運作良好,在其他情況下,再加上,你可以添加一個(或DateTime.MaxValue如果不需要),這是一個示例代碼:

public static string Encrypt(string content, DateTime expiration) 
{ 
    return FormsAuthentication.Encrypt(new FormsAuthenticationTicket(1, 
     HttpContext.Current.Request.UserHostAddress, // or something fixed if you don't want to stick with the user's IP Address 
     DateTime.Now, expiration, false, content)); 
} 

public static string Decrypt(string encryptedContent) 
{ 
    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(encryptedContent); 
    if (!ticket.Expired) 
      return ticket.UserData; 

    return null; // or throw... 
} 
相關問題