当前位置: 澳门新濠3559 > 操作系统 > 正文

当该用户再次访问您的网站时,第二种方法

时间:2019-12-21 19:57来源:操作系统
HttpCookiecookietwo=newHttpCookie("AccountsInfo");cookietwo.Expires=DateTime.Today.AddDays(360);cookietwo.Values["UserId"]=StringUtil.Repl(txtUserID.Text);cookietwo.Values["PassWord"]=txtPassword.Text;Response.Cookies.Add(cookietwo);和Requ

HttpCookiecookietwo=newHttpCookie("AccountsInfo");cookietwo.Expires=DateTime.Today.AddDays(360);cookietwo.Values["UserId"]=StringUtil.Repl(txtUserID.Text);cookietwo.Values["PassWord"]=txtPassword.Text;Response.Cookies.Add(cookietwo);和Request.Cookies.Add都能添加HttpCookie。但是结果完全不一样。Request.Cookies.Add要添加是什么。添加后根本存不下来。还有最近微信浏览器也存不住HttpCookie,是Response.Cookies.Add(cookietwo)添加的。请指点

增加:

   Cookie (HttpCookie的实例)提供了一种在 Web 应用程序中存储用户特定信息的方法。例如,当用户访问您的站点时,您可以使用Cookie 存储用户首选项或其他信息。

第一种方法:Response.Cookies[“UserName”].Value=”张三”

当该用户再次访问您的网站时,应用程序便可以检索以前存储的信息。

Response.Cookies[“UserName”].Expires=DateTime.Now.AddDays;

 
  ASP.NET中的cookie:创建Cookie方法 (1)
澳门新濠3559,  Response.Cookies["userName"].Value = “admin";  
  Response.Cookies[“userName”].Expires = DateTime.Now.AddDays(1);   //如果不设置失效时间,Cookie信息不会写到用户硬盘,浏览器关闭将会丢弃。  

第二种方法:

  ASP.NET中的cookie:创建Cookie方法 (2)
  HttpCookie aCookie = new HttpCookie(“lastVisit”); //上一次访问时间  
  aCookie.Value = DateTime.Now.ToString();  
  aCookie.Expires = DateTime.Now.AddDays(1);  
  Response.Cookies.Add(aCookie); 

HttpCookie cookie=new HttpCookie(“UserName”);

 

cookie.Value=”张三”;

  ASP.NET中的cookie:访问Cookie方法(1)
  if(Request.Cookies["userName"] != null)  
  Label1.Text = Server.HtmlEncode(Request.Cookies["userName"].Value);访问Cookie方法(2)  
  if(Request.Cookies["userName"] != null)  
  {  
  HttpCookie aCookie = Request.Cookies["userName"];  
  Label1.Text = Server.HtmlEncode(aCookie.Value);  
  } 

cookie.Expires=DateTime.Now.AddDays;

  

Response.Cookies.Add;

  ASP.NET中的cookie:创建多值Cookie方法 (1)
  Response.Cookies["userInfo"]["userName"] = “admin";  
  Response.Cookies["userInfo"]["lastVisit"] = DateTime.Now.ToString();  
  Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(1);  ASP.NET中的cookie:创建多值Cookie方法 (2)
  HttpCookie aCookie = new HttpCookie("userInfo");  
  aCookie.Values["userName"] = “admin";  
  aCookie.Values["lastVisit"] = DateTime.Now.ToString();  
  aCookie.Expires = DateTime.Now.AddDays(1);  
  Response.Cookies.Add(aCookie); 

多值Cookie:

 

第一种方法:

  ASP.NET中的cookie:读取多值Cookie
  HttpCookie aCookie = Request.Cookies["userInfo"];  
  string userName=aCookie.Values[“userName”];  
  string lastVisit=aCookie.Values[“lastVisit”];    ASP.NET中的cookie:修改和删除Cookie
  不能直接修改或删除Cookie,只能创建一个新的Cookie,发送到客户端以实现修改或删除Cookie.
     本贴来自天极网群乐社区--

Response.Cookies[“UserInfo”][“userName”]=”张三”;

 

Response.Cookies[“UserInfo”][“pwd”]=”123”;

Response.Cookies[“UserInfo”].Expires=DateTime.Now.AddDays;

第二种方法:

HttpCookie cookie=new HttpCOokie(“UserInfo”);

cookie.Values(“userName”)=”张三”;

cookie.Values(“pwd”)=”123”;

cookie.Expires=DateTime.Now.AddDays;

Response.Cookies.Add;

删除:

删除单个Cookie

Response.Cookies[“userName”].Expires=DateTime.Now.AddDays;

删除所有的Cookie

HttpCookie aCookie;

String cookieName;

int count=Request.Cookies.Count;

for(int i=0;i<count;i++)

{

cookieName=Request.Cookies[i].Name;

aCookie=new HttpCookie(cookieName);

aCookie.Expires=DateTime.Now.AddDays;

Response.Cookies.Add;

}

删除单个子键

HttpCookie aCookie=Request.Cookies[“userInfo”];

aCookie.Values.Remove(“userName”);

aCookie.Expires=DateTime.Now.AddDays;

Response.Cookies.Add;

修改:

修改单个Cookie

Response.Cookies[“userName”].Value=”李四”;

Response.Cookies[“userName”].Expires=DateTime.Now.AddDays;

修改单个子键

Response.Cookies[“UserInfo”][“pwd”]=”234”;

Response.Cookies[“UserInfo”].Expires=DateTime.Now.AddDays;

读取:

If(Request.Cookies[“userName”]!=null)

{

Lable1.Text=Request.Cookies[“userName”].Value;

}

或者

If(Request.Cookies[“userName”]!=null)

{

HttpCookie cookie=Request.Cookies[“userName”];

Lable.Text=cookie.Value;

}

多值读取

If(Request.Cookies[“UserInfo”]!=null)

{

Lable1.Text=Request.Cookies[“UserInfo”][“userName”];

Lable2.Text=Request.Cookies[“UserInfo”][“pwd”];

}

读取所有的Cookie

for(int i=0;i<Request.Cookies.Count;i++)

{

aCookie=Request.Cookies[i];

string name=aCookie.Name;

string value=aCookie.Value;

}

编辑:操作系统 本文来源:当该用户再次访问您的网站时,第二种方法

关键词: