Cookies essay access to cookies in C # C # cookies in C # The creation of cookies in C #: __c#

Source: Internet
Author: User
Tags datetime httpcontext setcookie urlencode

Create a username cookie on the client with a value of GJY, valid for 1 days.
Method 1:
response.cookies["username"]. Value= "ZXF";
response.cookies["username"]. Expires=datetime.now.adddays (1);

Method 2:
System.Web.HttpCookie newcookie=new HttpCookie ("username");
Newcookie. Value= "Gjy";
Newcookie. Expires=datetime.now.adddays (1);
Response.appendcookie (Newcookie);


To create a cookie with a subkey:
System.Web.HttpCookie newcookie=new HttpCookie ("user");
Newcookie. values["username"]= "ZXF";
Newcookie. values["Password"]= "111";
Newcookie. Expires=datetime.now.adddays (1);
Response.appendcookie (Newcookie);

The reading of cookies:

No Child key reads:
if (request.cookies["username"]!=null)
{
Response.Write (Server.HTMLEncode (request.cookies["username"). Value));
}

Read with child key:
if (request.cookies["user"]!=null)
{
Response.Write (Server.HTMLEncode (request.cookies["user"] ["username"]. Value));
Response.Write (Server.HTMLEncode (request.cookies["user"] ["password"]. Value));

Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;


public class Cookie
{
///
Cookies Assign Value
///
Primary key
Key value
Effective days
///
public bool Setcookie (string strName, string strvalue, int strday)
{
Try
{
HttpCookie Cookie = new HttpCookie (strName);
Cookie.domain = ". xxx.com";//When you want to access across domains, specify a domain name for the cookie in the format. xxx.com
Cookie.expires = DateTime.Now.AddDays (strday);
Cookie.value = strvalue;
SYSTEM.WEB.HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (Cookie);
return true;
}
Catch
{
return false;
}
}

///
Read cookies
///
Primary key
///

public string GetCookie (string strName)
{
HttpCookie Cookie = System.web.httpcontext.current.request.cookies[strname];
if (Cookie!= null)
{
return Cookie.Value.ToString ();
}
Else
{
return null;
}
}

///
Delete Cookies
///
Primary key
///
public bool Delcookie (string strName)
{
Try
{
HttpCookie Cookie = new HttpCookie (strName);
Cookie.domain = ". xxx.com";//When you want to access across domains, specify a domain name for the cookie in the format. xxx.com
Cookie.expires = DateTime.Now.AddDays (-1);
SYSTEM.WEB.HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (Cookie);
return true;
}
Catch
{
return false;
}
}
}


Example:
Cookie cookie = new Cookie ();
Cookie.setcookie ("name", "AAA", 1);//Assignment
Cookie.getcookie ("name");//value
Cookie.delcookie ("name");//delete
Note: When the cookie in Chinese appears garbled, then in the store to Chinese code, such as Cookie.setcookie ("name", Server.URLEncode ("AAA"), 1, read the decoding can be


Also: As long as the cookie is not set to expire, the cookie automatically expires when the browser is closed

Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;


public class Cookie
{
///
Cookies Assign Value
///
Primary key
Key value
Effective days
///
public bool Setcookie (string strName, string strvalue, int strday)
{
Try
{
HttpCookie Cookie = new HttpCookie (strName);
Cookie.domain = ". xxx.com";//When you want to access across domains, specify a domain name for the cookie in the format. xxx.com
Cookie.expires = DateTime.Now.AddDays (strday);
Cookie.value = strvalue;
SYSTEM.WEB.HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (Cookie);
return true;
}
Catch
{
return false;
}
}

///
Read cookies
///
Primary key
///

public string GetCookie (string strName)
{
HttpCookie Cookie = System.web.httpcontext.current.request.cookies[strname];
if (Cookie!= null)
{
return Cookie.Value.ToString ();
}
Else
{
return null;
}
}

///
Delete Cookies
///
Primary key
///
public bool Delcookie (string strName)
{
Try
{
HttpCookie Cookie = new HttpCookie (strName);
Cookie.domain = ". xxx.com";//When you want to access across domains, specify a domain name for the cookie in the format. xxx.com
Cookie.expires = DateTime.Now.AddDays (-1);
SYSTEM.WEB.HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (Cookie);
return true;
}
Catch
{
return false;
}
}
}


Example:
Cookie cookie = new Cookie ();
Cookie.setcookie ("name", "AAA", 1);//Assignment
Cookie.getcookie ("name");//value
Cookie.delcookie ("name");//delete
Note: When the cookie in Chinese appears garbled, then in the store to Chinese code, such as Cookie.setcookie ("name", Server.URLEncode ("AAA"), 1, read the decoding can be


Also: As long as the cookie is not set to expire, the cookie automatically expires when the browser is closed

Two

How to create and read cookies in C # clicknum:187| replynum:0

Write:
The code is as follows:
HttpCookie cookie = new HttpCookie ("Id_admin_");
Cookie. Value = Model.id_admin_. ToString ();
Cookie. Domain = ". Sosuo8.com";
HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (cookie);
cookie = new HttpCookie ("Name_admin_");
May be Chinese characters, must be encoded
Cookie. Value = Httputility.urlencode (Model.name_admin_);
Cookie. Domain = ". Sosuo8.com";
HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (cookie);
cookie = new HttpCookie ("GUID");
Cookie. Value = Guid.NewGuid (). ToString ();
Cookie. Domain = ". Sosuo8.com";
HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (cookie);

Read:
The code is as follows:
Httpcontext.current.request.cookies["GUID"]. Value

asp.net Empty cookies empty Individual
Copy code code as follows:
response.cookies["Admin"]. Expires = DateTime.Now.AddDays (-1);

asp.net empty cookies empty all
Request.Cookies.Clear () This method does not delete cookies
deleting cookies (that is, physically removing cookies from the user's hard disk) is a form of modifying cookies.
Because the Cookie is on the user's computer, it cannot be removed directly.
However, you can have your browser remove cookies for you.
The technique is to create a new cookie with the same name as the cookie you want to delete.
and set the expiration date of the Cookie to a date earlier than the current date.
When the browser checks the expiration date of the cookie, the browser discards the cookie that is now expired.


The following code example demonstrates a way to delete all available cookies in an application:
The code is as follows:
HttpCookie Acookie;
String CookieName;
int limit = Request.Cookies.Count;
for (int i = 0; i < limit; i++)
{
CookieName = Request.cookies[i]. Name;
Acookie = new HttpCookie (cookiename);
Acookie.expires = DateTime.Now.AddDays (-1);
RESPONSE.COOKIES.ADD (Acookie);
}

(my)

///<summary>
///login
///</summary>
///<param name= "sender" ></PARAM>
/// <param name= "E" ></param>
protected void Ibtnlogin_click (object sender, ImageClickEventArgs e)
        {
   HttpCookie cookie = new HttpCookie ("Ys_userinfo", CommonClass.Security.EncryptDES (Loginmod. Id.tostring (), "1shangkj"));
                Response.setcookie (cookie);
                Response.Redirect ("indexuser.aspx");
       }

<summary>
Exit
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
protected void Ibtngo_click (object sender, ImageClickEventArgs e)
{
if (httpcontext.current.response.cookies["Ys_userinfo"]!= null)
{
httpcontext.current.response.cookies["Ys_userinfo"]. Expires = DateTime.Now.AddDays (-1);//Set Expiration
}
Response.Redirect ("index.aspx");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.