Cookies have three properties to note:
. Domain domains
. Path paths
. Expires Expiration Time
Domain properties need to be set for cross-domain operations:
Response.Cookies ("MyCookie"). Domain = "Jb51.net"; (this refers to the generic domain name)
This can be accessed under Other level two domain names, ASP and asp.net test through
Virtual Directory Access:
I did a test on the ASP side,. NET is not tried, if you do not specify the path attribute, cookies cannot be shared under different virtual directories
Will Response.Cookies ("MyCookie"). Path = "/" It's okay.
The general wording:
Copy Code code as follows:
Response.Cookies ("MyCookie"). Domain = "Jb51.net";
Response.Cookies ("MyCookie"). Path = "/"
Response.Cookies ("MyCookie"). Expires = Now + 365;
Response.Cookies ("MyCookie") ("test") = "test";
. NET Clear Cookies
Copy Code code as follows:
HttpCookie cookie = System.web.httpcontext.current.request.cookies[cookiename];
if (cookie!= null)
{
Cookie. Values.clear ();
Setusercookieexpiretime (CookieName,-1);
Cookie. Domain = _domain;
System.Web.HttpContext.Current.Response.Cookies.Set (cookie);
}
public static void Setusercookieexpiretime (string key, int days)
{
System.web.httpcontext.current.response.cookies[key]. Domain = _domain;
System.web.httpcontext.current.response.cookies[key]. Path = _cookiepath;
System.web.httpcontext.current.response.cookies[key]. Expires = DateTime.Now.AddDays (days);
}
. NET Add/Update cookies
Copy Code code as follows:
public static void Addusercookies (string key,string value, string cookiename, String domain)
{
HttpCookie cookie = System.web.httpcontext.current.request.cookies[cookiename];
if (cookie = null)
{
cookie = new HttpCookie (cookiename);
Cookie. domain = domain;
Cookie. Path = _cookiepath;
Cookie. Values.add (key, value);
HttpContext.Current.Response.AppendCookie (cookie);
}
Else
{
if (system.web.httpcontext.current.request.cookies[cookiename). Values[key]!= null)
{
Cookie. Values.set (key, value);
}
Else
{
Cookie. domain = domain;
Cookie. Path = _cookiepath;
Cookie. Values.add (key, value);
HttpContext.Current.Response.AppendCookie (cookie);
}
}
}
Authentication cookie domain, what does that mean?
By default, a Cookie is associated with a specific domain. For example, if your site is www.jb51.net, the cookies you write are sent to the server when a user requests a page from that site. (except for cookies with a specific path value.) If your site has subdomains (such as jb51.net, S.jb51.net, and tools.jb51.net), you can associate cookies with specific subdomains. To do this, you need to set the Domain property of the Cookie, as follows:
Copy Code code as follows:
Response.Cookies ("Domain"). Value = DateTime.Now.ToString
Response.Cookies ("Domain"). Expires = DateTime.Now.AddDays (1)
Response.Cookies ("Domain"). Domain = "S.jb51.net"
If you set the domain in this way, the Cookie can only be used to specify pages in the child domain.
You can also use the Domain property to create cookies that can be shared across multiple child domains. For example, set the fields as follows:
Copy Code code as follows:
Response.Cookies ("Domain"). Value = DateTime.Now.ToString
Response.Cookies ("Domain"). Expires = DateTime.Now.AddDays (1)
Response.Cookies ("Domain"). Domain = "Jb51.net"
This allows the Cookie to be used for the primary domain, s.jb51.net, and Tools.jb51.net.