加密保存在Cookie中的数据

 答案:

利用FormsAuthentication类的HashPasswordForStoringInConfigFile方法可以对数据进行加密,可以指定加密算法,例如md5SHA1

本实例加密保存在Cookie中的数据。运行结果如图1所示。

1  加密保存在Cookie中的数据

关键技术

FormsAuthentication类的HashPasswordForStoringInConfigFile方法。

实现过程

1)新建一个网站,默认主页名为Default.aspx

2)在Default.aspx页面中的后台文件中编写代码。

protected void Page_Load(object sender, EventArgs e)

    {

        string str = "云计算";

        Response.Write("加密前的数据:");

        Response.Write(str);

        Response.Write("<br/>");

        Response.Write("md5加密后的数据:");

        Response.Cookies["data"].Value = FormsAuthentication.HashPasswordForStoringInConfigFile(str,"md5");

        Response.Write(Request.Cookies["data"].Value);

 

    }