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

你可能感兴趣的文章
手机H5显示一像素的细线
查看>>
Menu 菜单栏
查看>>
Integer跟int的区别(备份回忆)
查看>>
集合解析
查看>>
详解分布式应用程序协调服务Zookeeper
查看>>
软件工程之构建之法
查看>>
scrollView + tableview 上下滑动失效
查看>>
UVa 10902
查看>>
Mathf.Sin正弦
查看>>
禁止浏览器缓存js
查看>>
【Redis】安装PHP的redis驱动(二)
查看>>
java中string和int互相转化
查看>>
什么是序列化,为什么要序列化
查看>>
Java保留小数点后有效数字
查看>>
CommonHelper
查看>>
excel操作for(lutai)
查看>>
Codeforces Round #162 (Div. 2) C. Escape from Stones
查看>>
2017 Multi-University Training Contest - Team 4 phone call(树+lca+并查集)
查看>>
Struts2文件下载
查看>>
error LNK2019: 无法解析的外部符号 _PhInitializePhLib,该符号在函数 _EnumHandle 中被引用...
查看>>