asp.net cookies-cross-domain, virtual directory, etc. settings method _ Practical Tips

Source: Internet
Author: User
Tags httpcontext
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.
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.