I am trying to make a call to wcf function using ajax as below:
$.ajax({
url:http://localhost:64121/Test.svc/json/GetNumber?X='+ var1 + '&callback=?',
dataType: 'json',
beforeSend : function(xhr) {
xhr.setRequestHeader("Authorization", "BasicAuthTest");
},
success: function(trackingData) {
alert("success");
}
});
WCF code is beaking and is receving the request
public class CustomUserNameValidatorBasic : ServiceAuthorizationManager
{
protected override bool CheckAccessCore(OperationContext operationContext)
{
try
{
var msg = operationContext.RequestContext.RequestMessage;
// If user requests standart help-page then ignore authentication check.
if (msg.Properties.ContainsKey("HttpOperationName") && msg.Properties["HttpOperationName"].ToString() == "HelpPageInvoke")
{
return base.CheckAccessCore(operationContext);
}
var httpRequestHeaders = ((HttpRequestMessageProperty) msg.Properties[HttpRequestMessageProperty.Name]).Headers;
// Is Authorization-header contained in http-headers?
if (!httpRequestHeaders.AllKeys.Contains(HttpRequestHeader.Authorization.ToString()))
{
//code returns here
return false;
}
return false;
}
catch (Exception e)
{
return false;
}
}
}
As shown "Authorization" header is not included in req header array

In Firebug Under Request Headers for ajax reqeuest:
Request Headersview source Accept / Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5 Cache-Control max-age=0
Connection keep-alive Host test.proxyU.com If-Modified-Since Wed, 21 Mar 2012 19:46:56 GMT If-None-Match "e0818-17278-4bbc60dc86c00"
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0)
Gecko/20100101 Firefox/16.0
Update: Considering Answer below I have tried 3 Methods as below none of them adds the header. All call wcf correctly but `Authorization' header is just not in the request. And if I watch the http request the header looks very much like the screen shot I posted above
Method 1
$.ajax({
url: "http://localhost:64121/Test.svc/json/GetNumber?X='+ var1 + '&callback=?'",
beforeSend: function(xhr){xhr.setRequestHeader("Authorization", "BasicAuthTest");},
success: function(trackingData) {
alert("success");
}
});
Method 2
$.ajax({
url: "http://localhost:64121/Test.svc/json/GetNumber?X='+ var1 + '&callback=?'",
headers: {"Authorization": "BasicAuthTest"},
success: function(trackingData) {
alert("success");
}
});
Method 3
$.ajax({
url: "http://localhost:64121/Test.svc/json/GetNumber?X='+ var1 + '&callback=?'",
beforeSend : function(xhr, settings) {
$.extend(settings, { headers : { "Authorization" : "BasicAuthTest" } });
},
success: function(trackingData) {
alert("success");
}
});
Access-Control-Request-Headers:accept, authorization, originAuthorizationbeing entered and it works fine. But from javascript/jquery it appears to be adding the header insideAccess-Control-Request-Headers