A generic cookie action class under. Net

Source: Internet
Author: User
Tags datetime map class tostring

I have been involved in the development of a large multi-user mall, a lot of system user role, there are buyers, sellers, agents, system administrators, ordinary administrators, super administrators, etc., these users are involved in the landing system, and then create the problem of cookies, because of the different roles, these users login system, The cookies are relatively independent, and for a multi-user mall, cookies are important to differentiate the domain, different domain names to build different domains. So the author in the process of development, wrote many times to build cookies, take cookies code, annoying!

Since all are to build cookies, then there must be something in common, why don't we abstract out, write a generic cookie operation class, the following author takes. Net as an example, to explain how to implement this class!

Here is the function to create cookies

public void Setcookies (string domain, String type, Xiegou.xgmap map)
{
HttpCookie cookie = new HttpCookie (type); Defines the cookie object
DateTime dt = datetime.now;//defines the time object
TimeSpan ts=new TimeSpan (1,0,0,0);//cookie effective time, specifically check MSDN
cookies. Expires = dt. Add (ts);//Adds the action time
cookie. domain = domain;
for (int i =0 i < map. Size (); i++)
{
cookie. Values.add (map. Gettag (i), map. GetValue (i). ToString ());//Add Property
}
page. Response.appendcookie (cookie);//OK write to Cookie
}

Here is the function to take cookies

public string GetCookies (string type, string name)
{
if (page). Request.cookies[type]!= null)
{
if (page). Request.cookies[type]. Values[name]!= null)
{return
page. Request.cookies[type]. Values[name].  ToString ();
else
{return
"0";
}
return
"0";
}

Let's look at how to call these two functions:

Xiegou.xgmap map = new Xgmap (); The map class used here can refer to the online data to write a
map. ADD ("Shopid", shop.m_id.  ToString ()); Add map corresponding to table map
. ADD ("Shopname", Shop.m_name. ToString ());
Func. Setcookies (". shopxg.com", "Shopxg_namespace", map);

The principle is very simple, first define a map class, the name and value of the cookies to be built in the map table, and then unified by the setcookies to build cookies, the advantage of this is that you can write a lot of code, especially the need to frequently build cookies site!

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.