I have an asp.NET WCF on .NET 4. This service is used to authenticate users. We are submitting a username and password and then an HTTP Header should be returned with the authentication cookie included. Using a locally hosted test page this is working correctly. I am now attempting to access the header information cross domain. I have installed my test page on a different machine and configured to call across to the WCF. The call is working and the 'data' reply in the call is correct. However, I am unable to access the header information with either of the following:
alert(xmlHttp.getAllResponseHeaders());
or
alert(xmlHttp.getResponseHeader("Set-Cookie"));
Using the debugger in IE and the 'Live HTTP Header' plugin for Firefox, I can see the header information is being returned.
In my global ajax page I am setting the response to handle CORS.
private void EnableCrossDomainAjaxCall()
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
This is the AJAX I am using to call the service:
$("#btnLogin").click(function(e) {
var geturl;
geturl = $.ajax({
// type: "POST",
type: "GET",
contentType: "application/json; charset=utf-8",
url: 'http://10.0.4.66/AuthenticationService.svc/Login?Name=test&password=pwsd',
// url: '../SecurityServer/AuthenticationService.svc/Login?Name=test&password=pwsd',
dataType: "jsonp",
error: function(request, status, error) {
alert('Error Occured');
},
crossdomain: true,
success: function(data, textStatus, xmlHttp) {
// alert(xmlHttp.getResponseHeader("Content-Type"));
document.write(xmlHttp.getResponseHeader("Content-Type") + "<br/>");
alert(xmlHttp.getAllResponseHeaders());
alert(xmlHttp.getResponseHeader("Set-Cookie"));
var headers = '';
var headerPair = xmlHttp.getAllResponseHeaders('wcfCookie').split("\r\n");
var output = '';
$.each(headerPair, function(key, line) {
var parts = line.split(':');
if (parts[0] == 'wcfCookie') {
ChocChip = parts[1]
return false
}
});
}
});
Below is my header information grabbed from 'Live HTTP headers'
Date: Mon, 04 Feb 2013 12:12:40 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 4.0.30319
Access-Control-Allow-Origin: *
Set-Cookie: wcfCookie=8D38D5D6A0F138FEB595DD016F7694EDDF3E6757C82ED3D419F5047A5294974C1885487465CEC0A0BCC2B3802C7B03FF9F5370A05D4CCBDDDABCB1558C3816044BF4F78209BF38C6B1A7CAD34CD3C85C40B8515CFB1C2B2694BC78803D8DACB4
Content-Length: 65
Cache-Control: application/json; charset=utf-8
Content-Type: application/x-javascript