I am storing the token in request header using ajax and sending it to Rest web api. This is my request sent to web api:
var xhr = new XMLHttpRequest();
$.ajax({
url: 'http://localhost:32253/api/UserDetail/Authenticate',
headers: {
"Authorization-Token": res,
"X-HTTP-Method-Override": "GET"
},
type: 'GET',
async: false,
contentType: "application/json",
dataType: 'json',
success: function (data) {
alert("Success from success callback!");
// ShowData(data);
$('#RId').text(data.RoleId);
$('#RDesc').text(data.RoleDescription);
$('#RName').text(data.RoleName);
},
error: function (xhr, status) {
alert(status);
}
//complete: function (data) {
// alert("Success!from complete function");
// }
});
On the server side(rest web api), I am trying to read the header
if (Request.Headers.Contains("Authorization-Token"))
{
var token = Request.Headers.GetValues("Authorization-Token").First();
}
But the request does not contain the header "Authorization-Token". I can see the header name in Access-Control-request-Headers. I do not know how to read its value. Can someone help me out. I have enabled cors too
UPDATE Now i am passing the token using standard Authorization header of request object
$.ajax({
url: 'http://localhost:32253/api/UserDetail/Authenticate',
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Basic " + res);
},
type: 'GET',
async: false,
contentType: "application/json",
dataType: 'json',
authorization: res,
success: function (data) {
alert("Success from success callback!");
// ShowData(data);
$('#RId').text(data.RoleId);
$('#RDesc').text(data.RoleDescription);
$('#RName').text(data.RoleName);
},
error: function (xhr, status) {
alert(status);
}
//complete: function (data) {
// alert("Success!from complete function");
// }
});
but i cannot find it in request headers. See the image for more details Request header
REQUEST LOG This is the request received at the server side
OPTIONS /api/UserDetail/Authenticate HTTP/1.1
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en;q=0.5
Host: localhost:32253
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0
Access-Control-Request-Method: GET
Access-Control-Request-Headers: authorization-token,content-type
Origin: http://localhost:14576