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

你可能感兴趣的文章
ADODB.Stream在进行文件上传时报错
查看>>
5.5 数据库约束
查看>>
[Angularjs]单页应用之分页
查看>>
js---对象 和 函数this
查看>>
VC 6.0的简单使用
查看>>
IE6 兼容问题总结
查看>>
[转载] 民兵葛二蛋——第17集
查看>>
[转载] 七龙珠第一部——第004话 掳人的妖怪——乌龙
查看>>
【leetcode】75.Sort Colors
查看>>
2017-2018-1 20155332 《信息安全系统设计基础》课程总结
查看>>
strak组件(8):基本增删改查实现及应用和排序
查看>>
汕头市队赛 SRM 09 C 撕书
查看>>
Xcode常用快捷键
查看>>
如何快速有效的投诉上海移动
查看>>
python3爬虫-下载网易云音乐,评论
查看>>
【文文殿下】[BZOJ4008] [HNOI2015] 亚瑟王
查看>>
31.图片放大镜插件——jqzoom
查看>>
addSubview和insertSubview的区别
查看>>
js___原生js轮播
查看>>
堆排序—Java
查看>>