跨子域javascript陷阱设置Cookie

Jam*_*ars 6 javascript cookies asp.net-mvc

启用跨子域使用cookie时需要帮助.无法在javascript中将cookie设置为正确的值.我不确定Javascript是否未能设置cookie或MVC.NET拒绝请求cookie.

浏览器无法正常工作

  • Chrome 43(Windows)
  • Firefox 38(Windows)
  • iOS 8 Safari

设置我web.config使用的<httpCookies domain=".adomain.com" />东西开始出现可怕的错误.

我有一些javascript代码,与pickadate.js datepicker结合使用,它将cookie值更改为用户选择的日期.

Javascript函数

// Call pickadate API to retrieve selected date
var dateString = this.get('select', 'dd/mm/yyyy');

var cd = new Date();
var exp = cd.setMinutes(cd.getMinutes() + 10)

setCookie("_date", dateString, new Date(exp), "/", ".adomain.com");

window.location.reload();

function setCookie(name, value, expires, path, theDomain, secure) {
    value = escape(value);
    var theCookie = name + "=" + value +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((theDomain) ? "; domain=" + theDomain : "") +
    ((secure) ? "; secure" : "");
    document.cookie = theCookie;
}
Run Code Online (Sandbox Code Playgroud)

.NET在收到请求时正在做什么 一旦更改了datepicker,它将刷新到页面,在cookie中发送带有日期的新请求.这是一个MVC.NET控制器.但是,cookie在客户端没有变化.

    if(this.ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains("_date"))
{
     cookie.Value =   this.ControllerContext.HttpContext.Request.Cookies[sessionDate].Value;

     // Do some logic with date to retrieve products

} else {
     // Set cookie.value to today's date
}

cookie.HttpOnly = false;
cookie.Path = "/";
cookie.Secure = true;

this.ControllerContext.HttpContext.Response.Cookies.Set(cookie);
Run Code Online (Sandbox Code Playgroud)

http请求包含_date的以下副本:

_date=30/07/2015; 
_date=31/07/2015; 
Run Code Online (Sandbox Code Playgroud)

但是日期应该等于2015年7月31日,但我有重复.chrome resouce选项卡中的域名不同.

_date = 30/07/2015; domain = .adomain.com <<我需要这个域设置_date = 30/07/2015; 域= sub.adomain.com

Rah*_*ani 5

虽然我不是.NET专家,但可以在Set-Cookie标头中明确指定cookie的域.根据RFC 6265,如果您在标头中指定域,example.com那么cookie也可用于www.example.comsubdomain.example.com.子域不被视为外部域,因此不是安全违规.

在控制器中发送cookie之前可能会添加类似的东西

cookie.Domain = "adomain.com";