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!