Utilizing Ajax, I am storing the token in the request header and sending it to a Rest API. Here is the request sent to the 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);
}
});
When attempting to read the header on the server-side (Rest API), I encounter an issue.
if (Request.Headers.Contains("Authorization-Token")) {
var token = Request.Headers.GetValues("Authorization-Token").First();
}
However, the request does not contain the "Authorization-Token" header. After enabling CORS, I noticed the header name in Access-Control-request-Headers but struggled to determine how to read its value. Any assistance would be greatly appreciated.
UPDATE: I have now opted to pass the token using the standard Authorization header of the 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);
}
});
Despite this change, I am unable to locate the token in the request headers. For more information, please refer to the image below:
REQUEST LOG: Here 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