【问题标题】:How can i encrypt query string parameters in ASP.NET website? [duplicate]如何加密 ASP.NET 网站中的查询字符串参数? [复制]
【发布时间】:2011-03-15 05:54:17
【问题描述】:

在我的一个 ASP.Net 网站中,我必须向用户提供一个链接,在该链接中所有查询字符串参数都应加密。

我在想的是使用命令"aspnet_regiis"(用于加密web.config 数据),将输出作为查询字符串传递到已发布的网址中。

当用户单击该链接时,我首先解密字符串,然后获取查询字符串的原始数据。

我这样做对吗?有没有什么好的技术来加密和解密查询字符串?

【问题讨论】:

标签: asp.net encryption query-string


【解决方案1】:

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

它似乎只适用于 cookie,但在其他情况下效果很好,此外,您还可以添加过期日期(如果不需要,也可以添加 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...
}

【讨论】:

    猜你喜欢
    • 2010-12-02
    • 1970-01-01
    • 2010-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多