Second-level domain sharing cookies

Second-level domain sharing cookies

 I don't like writing technical articles very much. On the one hand, I feel nothing, and on the other hand, I go to learn. It always felt like there was nothing to write about. However, it is definitely not good to not share. In the future, I will post the solutions to the problems encountered in the project, large and small, for your reference and to point out my shortcomings. Not much nonsense, let's get down to business.

 I haven't done much cross-domain requests before, and now I want to share cookies across second-level domain names, so I checked the data and wrote the following method.

copy code
        public static void WriteCookie(string strName, string key, string strValue,string domain)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
            {
                cookie = new HttpCookie(strName);
            }
            cookie.Domain = domain;
            cookie.Path = "/";
            cookie[key] = strValue;
            HttpContext.Current.Response.AppendCookie(cookie);
        }
copy code

Cross-domain sharing of cookies in second-level domains is easy to implement.

The parameter domain is the main domain name of the website (not in any secondary domain name format): if there is www.test.com, event.test.com.usercp.test.com, the domain parameter at this time is test.com

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326925074&siteId=291194637