Asp.Net编程中,用户状态维护的几种方法 ---(转自Pierce)

  下面对维持用户状态作简单的描述,如果要深入描述,写一本专业书籍也不过分,其中的使用技巧,需要在实践中获取。

Asp.Net 编程依赖Http协议,而Http协议本身是无状态的,如果要维护用户的状态,那么要采用一些技术方法,根据不同的状况,可以使用不同的方法。

维持用户状态的方法,如下:
查询字符串:在用户访问Web的地址中加入查询字符,可以标记用户状态,因为查询字符是可见的,所以不要用来存储密码等敏感信息;只要提供正确的查询字符,随时有效。
Cookies:可以在用户端存储少量的信息,但是浏览器可能不支持,或者关闭了此功能,所以使用前,要检测客户浏览器是否支持,才可以使用;Cookie的有效期可以设置,几分钟、几天、几月不等,可以根据需要,在未过期前,访问Web站点都是可用的。
ViewState:在Page页面使用ViewState属性存储变量,因为ViewState在Client-Server请求中传输,所以存储的变量不宜过多,否则会影响性能;另外,存储的变量是页面级的,只在本页面有效。
Session:在Server端存储当前用户定义的变量,要分辨用户,要使用Cookie或者查询字符来标记用户,另外,存储的用户变量要占用系统资源,可以根据配置,选择Session变量存储的位置,如本级,或者其他机器。作用范围是整个会话过程。
Application:用来存储整个Web站点的全局变量,对所有访问用户有效。作用范围是整个Web。

使用查询字符:
格式如webform1.aspx?name=user,使用方法Response.Redirect来实现:
private   void  Button1_Click( object  sender, System.EventArgs e)
{
// Redisplay this page with a QueryString
Response.Redirect("Webform1.aspx?name=user");
}

客户端可以使用QueryString方法来解析查询字符:
private   void  Page_Load( object  sender, System.EventArgs e)
{
// Display the query string.
Response.Write(Request.QueryString["name"]);
}

使用Cookie:
Cookie适合在客户端存储少量的数据,用来标示用户的特定信息,如姓名,爱好等等;因为浏览器可以不支持Cookie,所以使用时,应该先检测客户是否支持cookie:
private   void  Page_Load( object  sender, System.EventArgs e)
{
// Run the first time this page is displayed.
if(!IsPostBack)
// If the browser supports cookies.
if(Request.Browser.Cookies)
{
// Create a cookie.
HttpCookie cookUPrefs = new HttpCookie("UPrefs");
cookUPrefs.Value 
= "English";
// Add the cookie.
Response.Cookies.Add(cookUPrefs);
}

}

下面代码可以检查请求是否包含cookie,如果有,那么获取cookie的值:
private   void  Page_Load( object  sender, System.EventArgs e)
{
// Run the first time this page is displayed.
if(!IsPostBack)
// If the browser supports cookies.
if(Request.Browser.Cookies)
// Check if the UPrefs cookie exists
if(Request.Cookies["UPrefs"!= null)
// Save the value of the cookie.
Session["Lang"= Request.Cookies["UPrefs"].Value;
}

使用ViewState:
使用ViewState存储变量,是把变量以隐藏字段的形式放在页面中,同时要求变量可以别序列化,如果要存储复杂的变量,那么需要先把变量转换为字符串。
下面的代码是把TextBox中的文本加入到页面的Table中的Cell。
private   void  Button1_Click( object  sender, System.EventArgs e)
{
// Add text to the view state.
ViewState.Add(ViewState.Count.ToString(), TextBox1.Text);
}

private   void  Page_Load( object  sender, System.EventArgs e)
{
if (IsPostBack)
// For each item in the ViewState
foreach(StateItem staItem in ViewState.Values)
{
TableRow rowNew 
= new TableRow();
TableCell celNew 
= new TableCell();
// Set cell text.
celNew.Text = staItem.Value.ToString();
// Add cell to row.
rowNew.Cells.Add(celNew);
// Add row to table
Table1.Rows.Add(rowNew);
}

}

使用Application和Session:
使用Application和Session可以存储任何类型的对象,Scope在整个的WebApplication或者整个Session。
注意以下几点:
Application和Session建立对象时不做名称和类型检查,这部分工作需要开发者来做;
维持Session影响性能,可以在Application或者Page Level来关闭此功能;
Application建立的对象,只在当前进程有效,对于多进程或者多Web Application,每个Application维持自己的对象,不相互影响;
Session对象,依赖前面讲的Cookie或者QueryString来标示用户,需要知道存储的信息是哪个用户的,一般是采用生成SessionID,通过Cookie发给用户,用户访问时,在Cookie中提供这个SessionID,说明身份。

使用简单,但是容易出错,如:

Application[ " Uname " =   " Wombat " ;
Response.Write(Application[
" Unamme " ]);

为了避免这种状况,最简单的办法是建立一个页面的变量,在Page_Load事件中获取Application变量,然后作其他处理,最后在Page_UnLoad中更新Application变量的值。
string  mstrUname  =   "" ;
private   void  Page_Load( object  sender, System.EventArgs e)
{
// Check if state variable exists.
if(Application["Uname"!= null)
// Get state variable.
mstrUname = Application["Uname"].ToString();
// Set variable
mstrUname = "Wombat";
// Use variable.
Response.Write(mstrUname);
}

private   void  Page_UnLoad( object  sender, System.EventArgs e)
{
// Save the state variables back.
Application["Uname"= mstrUname;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值