How to use cookies in. Net

Source: Internet
Author: User

For example, create a cookie called ASPCN, which is a small fish that is irrigated with water.

HttpCookie cookie = new httpcookie["ASPCN"];
Cookies. Value = "irrigation small fish";
Response.appendcookie (cookie);

It's also easy to remove the cookie value

HttpCookie cookie = request.cookies["ASPCN"];
Cookievalue = cookie. Value;

There is no problem in storing multiple messages in a cookie. For example, add more information under a cookie named ASPCN

HttpCookie cookie = new HttpCookie ("ASPCN");
Cookies. Values.add ("webmaster", "irrigation small fish");
Cookies. Values.add ("writer", "Beige");
Cookies. Values.add ("LinkColor", "Blue");
Response.appendcookie (cookie);

It's just as easy to take out information

HttpCookie cookie = request.cookies["ASPCN"];
value1 = cookies. values["Webmaster"];
value2 = cookies. values["Writer"];

HttpCookie cookie = Request.cookies[strkey];
if (null = = cookie)
{
Cookie does not exist
}
Let's see if there's a write client



Cookie=new HttpCookie ("www.xx.org");
Cookies. Values.add ("Usertype", DdlLoginType.SelectedItem.Value);
Cookies. Values.add ("UserName", TbUserName.Text.Trim ());
Cookies. Values.add ("Password", TbPassword.Text.Trim ());
Cookies. Values.add ("Access", i.ToString ());
Response.appendcookie (cookie);
Check if the cookie has been written to the browser
Cookie = request.cookies["www.xx.org"];
if (cookie==null| | Cookies. ToString () = = "")
{
Store with session
Loginuser loginuser = new Loginuser ();
Loginuser.username = TbUserName.Text.Trim ();
Loginuser.password = TbPassword.Text.Trim ();
Loginuser.usertype = DdlLoginType.SelectedItem.Value;
Loginuser.access = i;
session["www.xx.org"] = Loginuser;
}

My previous code, no problem with




The >>> page was just refreshed once

If you debug, does the code get executed on all? By the "is" doing any validation?

>>> then the delete

>>>response.cookies.remove ("MyCookie");
>>>response.redirect ("....")//steering

>>> then the delete

HttpCookie cookie = request.cookies["MyInfo"];
if (cookie = null)
{
Cookies. Expires = System.DateTime.Now.AddDays (-1);
}
RESPONSE.COOKIES.ADD (cookie);

response.cookies["Logincode"]. Expires =datetime.now.addhours (1);
This is valid for 1 hours, and the same method can be analogous to a few minutes, a few seconds, and so on.

How to use cookies in. Net

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.