我是不怎么喜歡寫技術文章的,一方面會了感覺沒啥,另一方面不會就去學習。始終感覺是沒啥可寫的。但是沒有分享肯定是不好的,以后會把在項目中遇到的大大小小的問題的解決方案都貼出來,供大家參考和指出我的不足。廢話不多了,說正事。
以前沒怎么做過跨域的請求,現在要做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