c# - Authorization Header is null when making jQuery Ajax request -
i have went through pretty of articles can find , followed suggested , somehow when processing request on server side authorization header null.
the back-end asp.net mvc web api , i've implemented filter authentication. i've tried using postman , setup authorization header , have verified back-end code works.
however, can't seem make work via ajax call in jquery.
here jquery ajax request:
//do ajax call api $.ajax({ url: "http://localhost:55602/api/evaluatetableexpression?callback=?", type: "get", data: "json=" + escape(json.stringify({ "fdml": "hr.employee emp { emp.id, emp.firstname, emp.prsname }" })), datatype: "jsonp", beforesend: function(request) { request.setrequestheader("authorization", "basic " + btoa("master:servant")); }, success: function(data, status) { console.log(data); if (!data.errorsoccurred) { var header = data.value.header.columns; var records = processrecords(header, data.value.rows.rows); rendergrid(header, records, $("#employeegrid")); } }, error: function(xhr, textstatus, errorthrown){ console.log(errorthrown); } });
and below snippet of code processes request...
public override void onauthorization(httpactioncontext actioncontext) { // in case user authorized using forms authentication // check header basic authentication if (thread.currentprincipal.identity.isauthenticated) return; var authheader = actioncontext.request.headers.authorization; if (authheader != null) { if (authheader.scheme.equals("basic", stringcomparison.ordinalignorecase) && !string.isnullorwhitespace(authheader.parameter)) if (authenticateuser(authheader)) return; } handleunauthorizedrequest(actioncontext); }
when inspect actioncontext.request.header, can see authorization value null.
what doing wrong here? in advance.
edit: including settings in web.config file might figure out what's causing problem.
<httpprotocol> <customheaders> <add name="access-control-allow-origin" value="*" /> <add name="access-control-request-headers" value="authorization, content-type"/> <add name="access-control-allow-headers" value="content-type, authorization" /> <add name="access-control-allow-methods" value="post, get, options" /> </customheaders> </httpprotocol> <authentication mode="forms" />
edit: below raw http request
get /api/evaluatetableexpression?callback=jquery21109280835636891425_1403905133542&json=%7b%22fdml%22%3a%22hr.employee%20as%20emp%20%7b%20emp.id%2c%20emp.firstname%2c%20emp.prsname%20%7d%22%7d&_=1403905133544 http/1.1 host: localhost:55602 connection: keep-alive accept: / user-agent: mozilla/5.0 (windows nt 6.3; wow64) applewebkit/537.36 (khtml, gecko) chrome/35.0.1916.153 safari/537.36 referer: localhost/fdmweb accept-encoding: gzip,deflate,sdch accept-language: en-us,en;q=0.8
edit: seems has doing cross origin request. when comment out datatype: "jsonp" attribute on ajax call, can see authorization header being set. problem is, how set header when expecting jsonp result or doing cors?
to solve problem need this
- remove custom headers web.config
- add reference in web api project:
system.web.http.cors
add following line code inside register method of webapiconfig class.
config.enablecors();
add following attribute in controller(s)
[enablecors(origins: "", headers: "", methods: "*", supportscredentials = true)]
Comments
Post a Comment