使用Cookie对象保存用户自定义设置

httpcookie对象,是成键值对出现的。


Cookie对象的常用的几个属性:
Name:获取或设置Cookie的名称
Expires:获取或设置Cookie的过期时间


Cookie对象的几个常用方法:
Add:新增一个Cookie对象
Clear:清楚会话状态中的所有数据
Remove:删除会话中的项
Get:通过变量名或索引得到Cookie的变量值
GetKey:以索引值来获取Cookie的变量名称


页面设计:

< table  border ="1"  style ="width: 40%" >
            
< tr >
                
< td  colspan ="2"  style ="text-align: center" >
                    用户登录
</ td >
            
</ tr >
            
< tr >
                
< td >
                    用户名:
</ td >
                
< td  >
                    
< asp:TextBox  ID ="TextBox1"  runat ="server"   ></ asp:TextBox ></ td >
            
</ tr >
            
< tr >
                
< td >
                    密 码:
</ td >
                
< td  >
                    
< asp:TextBox  ID ="TextBox2"  runat ="server" ></ asp:TextBox ></ td >     
            
</ tr >
            
< tr >
                
< td  width ="34%" >
                    Cookie有效期:
</ td >
                
< td  style ="width: 336px" >< asp:RadioButtonList  ID ="RadioButtonList1"  runat ="server"  RepeatDirection ="Horizontal" >
                        
< asp:ListItem  Value ="1"  Selected ="True" > 1天 </ asp:ListItem >
                        
< asp:ListItem  Value ="2" > 1个月 </ asp:ListItem >
                        
< asp:ListItem  Value ="3" > 半年 </ asp:ListItem >
                        
< asp:ListItem  Value ="4" > 1年 </ asp:ListItem >
                    
</ asp:RadioButtonList ></ td >
            
</ tr >
            
< tr >
                
< td  colspan ="2"  style ="text-align: center" >
                    
< asp:Button  ID ="btnlog"  runat ="server"  Text ="登录"  OnClick ="btnlog_Click"   />
                    
< asp:Button  ID ="btnreg"  runat ="server"  Text ="注册"   /></ td >
            
</ tr >
        
</ table >

 

 


后台代码:

ExpandedBlockStart.gif 代码
protected   void  btnlog_Click( object  sender, EventArgs e)
{
string  name  =   " admin " ;
string  pass  =   " admin " ;
if  ( this .TextBox1.Text  ==   " admin "   &&   this .TextBox2.Text  ==   " admin " )
{
if  ( this .RadioButtonList1.SelectedValue.ToString()  ==   " 1 " )
{
this .Response.Cookies[ " UserName " ].Expires  =  DateTime.Now.AddDays( 1 );
this .Response.Cookies[ " Password " ].Expires  =  DateTime.Now.AddDays( 1 );
}
if  (RadioButtonList1.SelectedValue.ToString()  ==   " 2 " )
{
this .Response.Cookies[ " UserName " ].Expires  =  DateTime.Now.AddMonths( 1 );
this .Response.Cookies[ " Password " ].Expires  =  DateTime.Now.AddMonths( 1 );
}
if  (RadioButtonList1.SelectedValue.ToString()  ==   " 3 " )
{
this .Response.Cookies[ " UserName " ].Expires  =  DateTime.Now.AddYears( 1   /   2 );
this .Response.Cookies[ " Password " ].Expires  =  DateTime.Now.AddYears( 1   /   2 );
}
if  (RadioButtonList1.SelectedValue.ToString()  ==   " 4 " )
{
this .Response.Cookies[ " UserName " ].Expires  =  DateTime.Now.AddYears( 1 );
this .Response.Cookies[ " Password " ].Expires  =  DateTime.Now.AddYears( 1 );
}
this .Response.Cookies[ " UserName " ].Value  =   this .TextBox1.Text;
this .Response.Cookies[ " Password " ].Value  =   this .TextBox2.Text;
Response.Redirect(
" getcookie.aspx " );
}
else
{
Response.Redirect(
" cookie.aspx " );
}

}

 

转到“getcookie.aspx”页面中

protected   void  Page_Load( object  sender, EventArgs e)
{
this .lab.Text  =   " 欢迎 "   +   this .Request.Cookies[ " UserName " ].Value  +   " 回来 " ;
}

 

由于并非所有的浏览器都支持Cookie,并且数据信息是以文本的形式保存在客户端机器中,因此最好不要保存敏感的、为加密的数据,否则会影响到网站的安全性。

string  pass = " admin " ;
Response.Cookies[
" pass " ].Value = FormsAuthentication.HashPasswordForStoringInConfigFile(pass, " md5 " );

 

 

转载于:https://www.cnblogs.com/greatandforever/archive/2009/12/07/1618815.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值