问题描述
我在用asp.net做测试时发现设置cookie的domain属性后添加cookie,传值到其他页面时domain属性无法正常读出,但是其他属性都可以读出来,不知道为什么?测试代码如下:----------------------------------publicpartialclass_Default:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){//HttpCookiecookie=newHttpCookie("UserInfo");//cookie.Value="name";//cookie.Domain="domain";//Response.Cookies.Add(cookie);CookiesOperate.setCookie("UserNameCookie2","seek2",1,".127.0.0.1");}Button1.Click+=newEventHandler(Button1_Click);}voidButton1_Click(objectsender,EventArgse){//TextBox1.Text=Request.Cookies["UserNameCookie2"].Domain;Response.Redirect(@"//127.0.0.1/test/2.aspx");}}publicclassCookiesOperate{///<summary>///信息写入cookie///</summary>///<paramname="key">cookie名</param>///<paramname="value">cookie值</param>///<paramname="timeout">过期时间,0表示cookie默认的过期时间</param>publicstaticvoidsetCookie(stringkey,stringvalue,inttimeout,stringdomain){try{HttpCookiecookie=newHttpCookie(key);value=HttpUtility.UrlEncodeUnicode(value);cookie.Value=value;if(timeout!=0)cookie.Expires=DateTime.Now.AddDays(Convert.ToInt32(timeout));if(!String.IsNullOrEmpty(domain)){cookie.Domain=domain;}HttpContext.Current.Response.Cookies.Add(cookie);}catch{}}}-----------------------------------------------------------------------------------2.aspxpublicpartialclass_:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){Response.Write("Domain:"+Server.HtmlEncode(Request.Cookies["UserNameCookie2"].Domain));}}-----------------------------------------------------------------------还请专家解释!
解决方案
解决方案二:
浏览器根据Domain来判定传不传Cookies但不传Domain这个数据
解决方案三:
感谢楼上回复,是这样的我的站点里有多个2级域名。cookie代码如下---------------------问题:无法删除cookie==============================登陆时=====================================//保存一个月(默认)if(cookieExpires==2){//主站cookieUtilities.CookiesOperate.setCookie("UserNameCookie",Utilities.Security.MD5Encrypt(name),1,"diybl.com");//BBScookieUtilities.CookiesOperate.setCookie("BBSUserCookie",Utilities.Security.MD5Encrypt(name),1,"bbs.diybl.com");//如果在外包注册过if(wbUser!=0){///外包cookieUtilities.CookiesOperate.setCookie("WbUserCookie",Utilities.Security.MD5Encrypt(name),1,"trade.diybl.com");}}=========================信息写入cookie=========================================///<summary>///信息写入cookie///</summary>///<paramname="key">cookie名</param>///<paramname="value">cookie值</param>///<paramname="timeout">过期时间,0表示cookie默认的过期时间</param>publicstaticvoidsetCookie(stringkey,stringvalue,inttimeout,stringdomain){try{HttpCookiecookie=newHttpCookie(key);value=HttpUtility.UrlEncodeUnicode(value);cookie.Value=value;if(timeout!=0)cookie.Expires=DateTime.Now.AddMonths(Convert.ToInt32(timeout));if(!String.IsNullOrEmpty(domain)){cookie.Domain=domain;}HttpContext.Current.Response.Cookies.Add(cookie);}catch{}}///<summary>///获得cookie///</summary>///<paramname="key">cookie名</param>///<returns></returns>///publicstaticstringgetCookie(stringkey){if(HttpContext.Current.Request.Cookies[key]==null||HttpContext.Current.Request.Cookies[key].ToString()=="")returnnull;elsereturnHttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[key].Value);}///<summary>///设置cookie过期///</summary>///<paramname="key">cookie名</param>///<returns></returns>publicstaticvoidKillCookie(stringkey){HttpCookiecookie=System.Web.HttpContext.Current.Request.Cookies[key];cookie.Values.Clear();cookie.Expires=DateTime.Now.AddYears(-1000);stringcookieDomain=GetCookieDomain(key);//HttpCookiecookie=newHttpCookie(key);//cookie.Value=null;if(!String.IsNullOrEmpty(cookieDomain)){cookie.Domain=cookieDomain;}//cookie.Expires=DateTime.Now.AddYears(-1000);HttpContext.Current.Response.Cookies.Set(cookie);}///<summary>///返回要删除cookie的域///</summary>///<paramname="key"></param>///<returns></returns>privatestaticstringGetCookieDomain(stringkey){stringcookieDomain="";if(key=="UserNameCookie"){cookieDomain="diybl.com";}if(key=="BBSUserCookie"){cookieDomain="bbs.diybl.com";}if(key=="WbUserCookie"){cookieDomain="trade.diybl.com";}returncookieDomain;}===============================删除cookie=====================================CookiesOperate.KillCookie("UserNameCookie");CookiesOperate.KillCookie("BBSUserCookie");CookiesOperate.KillCookie("WbUserCookie");
现在的问题是用户无法正常注销,我测试后发现是没有办法获得Domain域。请问应该怎么改?
解决方案四:
你设置cookieDomain="diybl.com";所有的二级域名就可以用你调协的Cookies
解决方案五:
你设置cookieDomain="diybl.com";所有的二级域名就可以用你的Cookies
解决方案六:
我的问题是注销的时候没办法注销啊!
解决方案七:
注销代码看下
解决方案八:
就是最下面那段===============================删除cookie=====================================CookiesOperate.KillCookie("UserNameCookie");CookiesOperate.KillCookie("BBSUserCookie");CookiesOperate.KillCookie("WbUserCookie");