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

你可能感兴趣的文章
黄聪:在Windows下搭建***服务器
查看>>
git常用命令
查看>>
[Android学习笔记]EditText的使用
查看>>
《活出生命的意义》读后感
查看>>
第四次作业
查看>>
第二次软件综合实验专题作业——结对编程 1759103 李思佳 、 1759107 陶彦婷
查看>>
TCP协议三次握手过程分析
查看>>
std::lexicographical_compare
查看>>
Java设计模式:代理模式(一)
查看>>
直线栅格化(基于 Bresenham 算法)
查看>>
python IDLE 如何实现清屏
查看>>
三维系统应用实例——截图
查看>>
MVC4 + EF为Model添加单独的验证属性
查看>>
C# Dictionary 的几种遍历方法
查看>>
jsvascript === 和==的区别
查看>>
解决Ubuntu(乌班图)vi/vim模式下粘贴的代码内容会多出的空格的问题
查看>>
用递归的方式实现阶乘
查看>>
对局匹配
查看>>
浅析python 中__name__ = '__main__' 的作用
查看>>
(转)intellij idea svn 修改文件后,父文件夹也标注修改
查看>>