日期:2014-05-17  浏览次数:20396 次

关于.net中跨域名的cookies共享问题,困扰小弟一天,头疼!

  今天在公司处理问题时遇到一个问题,
  假设我主域名为 www.yuming.com 二级域名为 erji.yuming.com
  我现在在二级 登录时 创建了一个cookies保存验证码,
  创建方法如下:
public static void setCookie(string key, string value, int timeout)
  {
  try
  {
  HttpCookie cookie = new HttpCookie(key);
  value = HttpUtility.UrlEncode(value);
  cookie.Value = value; 
  cookie.Domain = ".yuming.com";
  cookie.Secure = false;
  if (timeout != 0)
  cookie.Expires = DateTime.Now.AddMinutes(Convert.ToInt32(timeout));
  HttpContext.Current.Response.Cookies.Add(cookie);
  }
  catch
  {
  }
  }

  -------------------
  然后提交时我直接把填写的登录账号密码 post到主站一个专门的页面进行处理
  问题就出现在:
  我在主站处理时 当需要获取保存在cookies中的分站验证码时,就无法获取到了。

  请各位大虾帮我看看!深感谢意!
 

------解决方案--------------------
代码这样写是没有问题的,可能其他的地方导致的。
你可以只采用这样的代码进行测试

Cookie1.aspx
public partial class Cookie1 : System.Web.UI.Page
{
public static void setCookie(string key, string value, int timeout)
{
try
{
HttpCookie cookie = new HttpCookie(key);
value = HttpUtility.UrlEncode(value);
cookie.Value = value;
cookie.Domain = ".yuming.com";
cookie.Secure = false;
if (timeout != 0)
cookie.Expires = DateTime.Now.AddMinutes(Convert.ToInt32(timeout));
HttpContext.Current.Response.Cookies.Add(cookie);
}
catch
{
}
}

protected void Page_Load(object sender, EventArgs e)
{
setCookie("name", "mxh", 30);
}
}

GetCookie.aspx
public partial class GetCookie : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Request.Cookies["name"].Value);
}
}
------解决方案--------------------
是不是路径权限的问题。path="/"