博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
操作Cookie公用代码
阅读量:5918 次
发布时间:2019-06-19

本文共 2851 字,大约阅读时间需要 9 分钟。

 

None.gif
#region关于操作Cookie的方法
None.gif
ExpandedBlockStart.gifContractedBlock.gif         
/**/
///<summary>
InBlock.gif
InBlock.gif         
///创建cookie值
InBlock.gif
InBlock.gif         
///</summary>
InBlock.gif
InBlock.gif         
///<param name="cookieName">cookie名称</param>
InBlock.gif
InBlock.gif         
///<param name="cookieValue">cookie值</param>
InBlock.gif
ExpandedBlockEnd.gif         
///<param name="cookieTime">cookie有效时间</param>
None.gif
None.gif         
private
 
void
 CreateCookieValue(
string
 cookieName,
string
 cookieValue,DateTime cookieTime)
None.gif
ExpandedBlockStart.gifContractedBlock.gif         
dot.gif
{
InBlock.gif
InBlock.gif              HttpCookie cookie 
= new HttpCookie(cookieName);
InBlock.gif
InBlock.gif              cookie.Value
=cookieValue;
InBlock.gif
InBlock.gif              
//DateTime dtNow = DateTime.Now ;
InBlock.gif
InBlock.gif              
//TimeSpan tsMinute = cookieTime;
InBlock.gif
InBlock.gif              cookie.Expires 
= cookieTime;
InBlock.gif
InBlock.gif              Response.Cookies.Add(cookie);
InBlock.gif
ExpandedBlockEnd.gif         }
None.gif
ExpandedBlockStart.gifContractedBlock.gif         
/**/
///<summary>
InBlock.gif
InBlock.gif         
///创建cookie值
InBlock.gif
InBlock.gif         
///</summary>
InBlock.gif
InBlock.gif         
///<param name="cookieName">cookie名称</param>    
InBlock.gif
InBlock.gif         
///<param name="cookieValue">cookie值</param>
InBlock.gif
InBlock.gif         
///<param name="subCookieName">子信息cookie名称</param>
InBlock.gif
InBlock.gif         
///<param name="subCookieValue">子信息cookie值</param>
InBlock.gif
ExpandedBlockEnd.gif         
///<param name="cookieTime">cookie有效时间</param>
None.gif
None.gif         
private
 
void
 CreateCookieValue(
string
 cookieName,
string
 cookieValue,
string
 subCookieName,
string
 subCookieValue,DateTime cookieTime)
None.gif
ExpandedBlockStart.gifContractedBlock.gif         
dot.gif
{
InBlock.gif
InBlock.gif              HttpCookie cookie 
= new HttpCookie(cookieName);
InBlock.gif
InBlock.gif              cookie.Value
=cookieValue;
InBlock.gif
InBlock.gif              cookie[subCookieName]
=subCookieValue;
InBlock.gif
InBlock.gif              cookie.Expires 
= cookieTime;
InBlock.gif
InBlock.gif              Response.Cookies.Add(cookie);
InBlock.gif
ExpandedBlockEnd.gif         }
None.gif
ExpandedBlockStart.gifContractedBlock.gif         
/**/
///<summary>
InBlock.gif
InBlock.gif         
///取得cookie的值
InBlock.gif
InBlock.gif         
///</summary>
InBlock.gif
InBlock.gif         
///<param name="cookieName">cookie名称</param>
InBlock.gif
ExpandedBlockEnd.gif         
///<returns></returns>
None.gif
None.gif         
private
 
string
 GetCookieValue(
string
 cookieName)
None.gif
ExpandedBlockStart.gifContractedBlock.gif         
dot.gif
{
InBlock.gif
InBlock.gif              
string cookieValue="";
InBlock.gif
InBlock.gif              HttpCookie cookie 
= Request.Cookies[cookieName];             
InBlock.gif
InBlock.gif              
if(null == cookie)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   cookieValue
="";
InBlock.gif
ExpandedSubBlockEnd.gif              }
InBlock.gif
InBlock.gif              
else
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   cookieValue
=cookie.Value;
InBlock.gif
ExpandedSubBlockEnd.gif              }
InBlock.gif
InBlock.gif              
return cookieValue;
InBlock.gif
ExpandedBlockEnd.gif         }
None.gif
ExpandedBlockStart.gifContractedBlock.gif         
/**/
///<summary>
InBlock.gif
InBlock.gif         
///取得cookie的值
InBlock.gif
InBlock.gif         
///</summary>
InBlock.gif
InBlock.gif         
///<param name="cookieName">cookie名称</param>
InBlock.gif
InBlock.gif         
///<param name="subCookieName">cookie子信息值</param>
InBlock.gif
ExpandedBlockEnd.gif         
///<returns></returns>
None.gif
None.gif         
public
 
static
 
string
 GetCookieValue(
string
 cookieName, 
string
 subCookieName)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
{
InBlock.gif            
string cookieValue = "";
InBlock.gif            HttpCookie cookie 
= HttpContext.Current.Request.Cookies[cookieName];
InBlock.gif            
if (null == cookie)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cookieValue 
= "";
ExpandedSubBlockEnd.gif            }
InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cookieValue 
= cookie.Value;
InBlock.gif                
string[] words = cookieValue.Split('&');
InBlock.gif                
foreach (string s in words)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif
InBlock.gif                    
if (s.IndexOf(subCookieName+"=">= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif
InBlock.gif                        cookieValue 
= s.Split('=')[1];
ExpandedSubBlockEnd.gif                    }
InBlock.gif
ExpandedSubBlockEnd.gif                }
InBlock.gif
InBlock.gif
InBlock.gif            
//  cookieValue = cookieValue.Split('&')[1].ToString().Split('=')[1];
ExpandedSubBlockEnd.gif
            }
InBlock.gif            
return cookieValue;
ExpandedBlockEnd.gif        }
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif         
/**/
///<summary>
InBlock.gif
InBlock.gif         
///删除某个固定的cookie值[此方法一是在原有的cookie上再创建同样的cookie值,但是时间是过期的时间]
InBlock.gif
InBlock.gif         
///</summary>
InBlock.gif
ExpandedBlockEnd.gif         
///<param name="cookieName"></param>
None.gif
None.gif         
private
 
void
 RemoteCookieValue(
string
 cookieName)
None.gif
ExpandedBlockStart.gifContractedBlock.gif         
dot.gif
{
InBlock.gif
InBlock.gif              
string dt="1900-01-01 12:00:00";
InBlock.gif
InBlock.gif              CreateCookieValue(cookieName,
"",Convert.ToDateTime(dt));
InBlock.gif
ExpandedBlockEnd.gif         }
None.gif
None.gif         
#endregion
None.gif
None.gif 
None.gif
None.gif以下是调用:
None.gif
None.gif 
None.gif
None.gif         
private
 
void
 Page_Load(
object
 sender, System.EventArgs e)
None.gif
ExpandedBlockStart.gifContractedBlock.gif         
dot.gif
{
InBlock.gif
InBlock.gif              
// 在这里放置使用者程序代码以初始化网页     
InBlock.gif
InBlock.gif              
this.txt_UserID.Text=this.GetCookieValue("UserName","UserID");//取得用户名
InBlock.gif
ExpandedBlockEnd.gif         }
None.gif
None.gif 
None.gif
None.gif         
private
 
void
 btn_Submit_Click(
object
 sender, System.EventArgs e)
None.gif
ExpandedBlockStart.gifContractedBlock.gif         
dot.gif
{    
InBlock.gif
InBlock.gif#region用Cookie进行保存登入用户名
InBlock.gif
InBlock.gif              
if(this.chb_IsSave.Checked)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   
//将用户保存一个小时,具体设置可以进行调整。。
InBlock.gif
InBlock.gif                   
//这里用了固定的公用的cookie用户UserName,用户编号UserID进行访问
InBlock.gif
InBlock.gif                   CreateCookieValue(
"UserName","UserName","UserID",this.txt_UserID.Text,DateTime.Now+new TimeSpan(0,1,0,0));//设置保存用户名
InBlock.gif
ExpandedSubBlockEnd.gif              }
InBlock.gif
InBlock.gif              
#endregion
InBlock.gif
ExpandedBlockEnd.gif}
None.gif
None.gif

转载地址:http://zhfvx.baihongyu.com/

你可能感兴趣的文章
debian、ubuntu系统下,常用的下载工具
查看>>
带以太网的MicroPython开发板:TPYBoardv201温湿度上传实例
查看>>
如何解压缩后缀名为zip.001,zip.002等的文件
查看>>
OSGI企业应用开发(十二)OSGI Web应用开发(一)
查看>>
Python 以指定概率获取元素
查看>>
微信公众平台图文教程(二) 群发功能和素材管理
查看>>
关于System.Collections空间
查看>>
MPP(大规模并行处理)
查看>>
Centos下基于Hadoop安装Spark(分布式)
查看>>
POJ2348 UVa10368 HDU1525 Euclid's Game【博弈】
查看>>
Java 位运算
查看>>
好用的CSS模块化打包工具CSS-COMBO
查看>>
python 中的字符和字符串
查看>>
C#Winform限制Textbox只能输入数字
查看>>
EL表达式经典用法
查看>>
java.lang.NoClassDefFoundError: javax/mail/Authenticator
查看>>
联想集团涨超7% 杨元庆持股比例升至8.12%
查看>>
各省光伏十三五规划汇总:总规模将超130GW
查看>>
Apache Storm 官方文档 —— 常用模式
查看>>
聊聊JVM的年轻代
查看>>