2010-07-13 95 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... 
} 
相关问题