`

二级域名共享Cookie

    博客分类:
  • web
阅读更多

二级域名共享Cookie

 我是不怎么喜欢写技术文章的,一方面会了感觉没啥,另一方面不会就去学习。始终感觉是没啥可写的。但是没有分享肯定是不好的,以后会把在项目中遇到的大大小小的问题的解决方案都贴出来,供大家参考和指出我的不足。废话不多了,说正事。

 以前没怎么做过跨域的请求,现在要做Cookie的跨二级域名共享,就查了查资料写出了如下方法。

复制代码
        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);
        }
复制代码

cookie在二级域名中跨域共享是很容易实现的。

参数domain为网站主域名(不为任何二级域名格式):如有www.test.com,event.test.com.usercp.test.com,此时的domain参数为test.com

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics