2014-12-13 75 views
1

我的劇本是AngularJS HTTP請求不發送會話cookie的CORS

GET http://example.com/api/Event HTTP/1.1 
Host: example.com 
Connection: keep-alive 
Cache-Control: max-age=0 
Accept: application/json, text/plain, */* 
Origin: http://www.example.com 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 
Referer: http://www.example.com/Home/Event 
Accept-Encoding: gzip, deflate, sdch 
Accept-Language: en-US,en;q=0.8 

我需要一個認證會話cookie來這個請求被髮送。

只有當網頁的網址前面沒有www時纔會發送cookie。

我在此網站上發現了類似的問題,答案是:

.config(function ($routeProvider, $httpProvider) { 
    $httpProvider.defaults.withCredentials = true; 
    //rest of route code 

但我在哪裏把這個代碼在我的劇本?

我正在開發Web應用程序與網頁API的ASP.NET應用程序MVC5 2.

回答

0

事實證明,我的AngularJS腳本工作正常(只需將{withCredentials:true}作爲$ http.get方法中的參數傳遞)。出錯的是我的MVC應用程序不會將會話cookie存儲在子域中。所以我通過在CookieAuthenticationOptions中添加CookieDomain來修復它。

0

該配置需要被注入角模塊,像這樣:

// get an existing module by name 
var app = angular.module('app'); 

// inject $httpProvider configuration 
app.config(function ($httpProvider) { 
    $httpProvider.defaults.withCredentials = true; 
});