二级域名共享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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM