Provide a. NET generic Cookie Action class

Source: Internet
Author: User

Intermediary transaction http://www.aliyun.com/zixun/aggregation/6858.html ">seo diagnose Taobao guest cloud host technology Hall

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 domains, 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 I take. 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);//define Cookie Object

DateTime dt = datetime.now;//Definition Time Object

TimeSpan ts=new TimeSpan (1,0,0,0)//cookie effective time, specifically check MSDN

Cookie. Expires = dt. Add (TS);

Cookie. domain = domain;

for (int i =0 i < map. Size (); i++)

{

Cookie. Values.add (map. Gettag (i), map. GetValue (i). ToString ());

}

Page. Response.appendcookie (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 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!

Above is the author's humble opinion, if there is a problem, welcome to exchange. Email:cxl@xiegoo.com.

Related Article

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.