Cross-Domain Cookie in ASP. NET

Source: Internet
Author: User

Address: http://www.cnblogs.com/greatverve/archive/2011/07/05/asp-net-cookie-domain.html

 

 

Restrict the valid range of cookies to the domain.
By default, cookies are associated with specific domains.
For example, if your site is www.contoso.com, when a user requests a page from the site,
The Cookie you wrote is sent to the server. (Except for cookies with specific path values)
If your site has subdomains (such as contoso.com, sales.contoso.com, and support.contoso.com ),
The Cookie can be associated with a specific subdomain. Therefore, you need to set the Domain attribute of the Cookie as follows: Response. Cookies ("domain"). Value = DateTime. Now. ToString
Response. Cookies ("domain"). Expires = DateTime. Now. AddDays (1)
Response. Cookies ("domain"). Domain = "support.contoso.com"

If you set the domain in this way, the Cookie can only be used to specify the page in the subdomain.
You can also use the Domain attribute to create cookies that can be shared in multiple sub-domains. For example, set Response. Cookies ("domain"). Value = DateTime. Now. ToString
Response. Cookies ("domain"). Expires = DateTime. Now. AddDays (1)
Response. Cookies ("domain"). Domain = "contoso.com"

In this way, the Cookie can be used for the primary domain, sales.contoso.com, and support.contoso.com.

Create a Cross-Domain Cookie.
For example, www.it100.info. All the second-level domain names under the root domain can share cookies, {mail.it100.info, photo.it100.info} public static bool CreateCookie (string strCookieName, string strCookieValue, string strDomain, bool blURLEncode)
{
If (blURLEncode)
{
StrCookieValue = System. Web. HttpContext. Current. Server. UrlEncode (strCookieValue );
}
HttpCookie objCookie = new HttpCookie (strCookieName, strCookieValue );
ObjCookie. Domain = strDomain; // set the Cookie Domain Name
System. Web. HttpContext. Current. Response. Cookies. Add (objCookie );
Return true;
}

Url: http://greatverve.cnblogs.com/archive/2011/07/05/asp-net-cookie-domain.html

Note the following three attributes of a Cookie:
1. Domain
2. Path
3. Expires expiration time

For cross-origin operations, you need to set the domain attributes:
Response. Cookies ("MyCookie"). Domain = "cnblogs.com"; (this refers to a wildcard Domain name)
In this way, access is available under other second-level domain names, and ASP. NET pass the test.

Access under the virtual directory:
I did a test on ASP, but I did not try. NET. If the Path attribute is not specified, Cookies cannot be shared under different virtual directories.
Set Response. Cookies ("MyCookie"). Path = "/".

General statement:
Response. Cookies ("MyCookie"). Domain = "cnblogs.com ";
Response. Cookies ("MyCookie"). Path = "/"
Response. Cookies ("MyCookie"). Expires = Now + 365;
Response. Cookies ("MyCookie") ("Test") = "test ";

. NET clear Cookie
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
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 );
}
}
}

This method implements Cross-Domain cookie cross-directory

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.