博客
关于我
强烈建议你试试无所不能的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/

你可能感兴趣的文章
unity, change shader for me and all my children
查看>>
两个实验操作系统-ubuntu在安装配置pintos
查看>>
Linux的proc文件系统
查看>>
基于CoreText的基础排版引擎之不带图片的排版引擎
查看>>
env-update干了些什么
查看>>
Mybatis在oracle、mysql、db2、sql server的like模糊查询
查看>>
struts1和struts2安全线
查看>>
论MySQL何时使用索引,何时不使用索引
查看>>
[TroubleShooting] The remote copy of database xx has not been rolled forward to a point in time
查看>>
Arcgis for Javascript实现图
查看>>
(10)Xamarin.Android - 储存数据于Windows Azure
查看>>
android4.0 USB Camera示例(四)CMOS
查看>>
Java NIO 系列教程
查看>>
Aittit.attilax超级框架 api 规划大全
查看>>
用java实现zip压缩
查看>>
FreeBSD包管理
查看>>
C基本语句测试
查看>>
前端的瑞士军刀:Modernizr.js
查看>>
Swift - iOS中各种视图控制器(View Controller)的介绍
查看>>
客户端获取服务端自定义类数据 z
查看>>