Session对象
•在ASP.NET中,Session的存储方式有三种
–使用那种存储方式是在Web.config文件中进行设置的,先来看Web.config中关于Session的一段代码片断:
•<sessionState mode="InProc" cookieless="false" timeout="20" />
–还有一种Mode的值为StateServer,这种模式的特别之处是,重新启动IIS时,所保存的Session值是不会丢失的。
–最后一种Mode属性的值为SQL Server,它是指该会话状态保存在有SQL Server数据库的计算机上,并且当数据库重启服务时,会话数据依然会保留。
利用Sesson传递用户登陆信息。核心代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
if (this.TextBox1.Text.Trim() != "")
{
Session["username"] = this.TextBox1.Text.Trim();
this.Response.Redirect("Default.aspx");
}
else
{
this.Literal1.Text = "请输入昵称!";
}
}
Cookies对象
•ASP.NET包含两个内部Cookie集合。HttpRequest的Cookies集合是从客户端传送到服务器的Cookie。HttpResponse的Cookies集合包含的是一些新Cookie,这些Cookie在服务器上创建然后传输到客户端。
•Cookie在使用方法上跟Session和Application不同。