odata - HOWTO add request header to breeze batch requests -
note not same thing adding header parent request mentioned in other tread: breeze - adding headers request.
i'm using breeze webapiodata dataservice & need add specific header value within each request within batch. can add header value parent request (using afore mentioned link), isn't included in child request. in specific instance, need include antiforgery token request. batch requests split out individual requests webapi odata controllers , such, don't see parent request. i'm exploring how tweak server side code in validates anti forgery token, (1) addresses specific challenge , (2) doesn't others may need include header value in request. i'm using breeze v1.4.12... looking @ source, appears headers being explicitly set (lines 15280-15284 & line 15380)... isn't clear me if possible override , control inner header individual requests within batch @ present time.
specifically, i'm controlling inclusion of antiforgery token in request breeze:
breeze.config.initializeadapterinstance('dataservice', 'webapiodata', true); var unsecuredclient = odata.defaulthttpclient; var securedclient = { request: function (request, success, error) { request.headers.requestverificationtoken = jquery("#__requestverificationtoken").val(); return unsecuredclient.request(request, success, error); } }; odata.defaulthttpclient = securedclient;
when @ request sent server, see token in primary request's header, not in inner request:
post https://localhost:44362/odata/$batch http/1.1 host: localhost:44362 connection: keep-alive content-length: 585 cache-control: no-cache pragma: no-cache requestverificationtoken: fnr1oqb3sxnlf5eui9mb-partial-rest-ommitted maxdataserviceversion: 3.0 origin: https://localhost:44362 user-agent: mozilla/5.0 (windows nt 6.3; wow64) applewebkit/537.36 (khtml, gecko) chrome/35.0.1916.114 safari/537.36 content-type: multipart/mixed;boundary=batch_2a9d-ba3e-cf5f accept: multipart/mixed dataserviceversion: 2.0 referer: https://localhost:44362/home/spa accept-encoding: gzip,deflate,sdch accept-language: en-us,en;q=0.8 cookie: asp.net_sessionid=jtfbzyt4zqk5pu43p1qop5tl; fedauth=ommitted --batch_2a9d-ba3e-cf5f content-type: multipart/mixed; boundary=changeset_1fec-c63e-5864 --changeset_1fec-c63e-5864 content-type: application/http content-transfer-encoding: binary post odata/projects http/1.1 content-id: 1 dataserviceversion: 2.0 accept: application/atomsvc+xml;q=0.8, application/json;odata=fullmetadata;q=0.7, application/json;q=0.5, */*;q=0.1 content-type: application/json maxdataserviceversion: 3.0 {"id":-1,"type":"sp.data.research_x0020_projectslistitem","etag":null,"title":"test"} --changeset_1fec-c63e-5864-- --batch_2a9d-ba3e-cf5f--
and when @ raw response, can see how fails find token in header (confirmed when step through code on server-side):
http/1.1 202 accepted cache-control: no-cache pragma: no-cache content-type: multipart/mixed; boundary=batchresponse_efc71196-045b-474c-bfe3-e21e20ae64b0 expires: -1 server: microsoft-iis/8.0 dataserviceversion: 3.0 x-aspnet-version: 4.0.30319 x-sourcefiles: =?utf-8?b?rdpcrgv2xfjlcg9zaxrvcmllc1xtc2z0c3bjyw1yavxzcmncu3bszxnlyxjjafryywnrzxjcb2rhdgfcjgjhdgno?= x-powered-by: asp.net date: thu, 29 may 2014 16:42:00 gmt content-length: 4432 --batchresponse_efc71196-045b-474c-bfe3-e21e20ae64b0 content-type: multipart/mixed; boundary=changesetresponse_aa3c98d6-a059-454d-9477-00e0b9497278 --changesetresponse_aa3c98d6-a059-454d-9477-00e0b9497278 content-type: application/http content-transfer-encoding: binary http/1.1 500 internal server error content-id: 1 content-type: application/json; odata=fullmetadata; charset=utf-8 dataserviceversion: 3.0 { "odata.error":{ "code":"","message":{ "lang":"en-us","value":"an error has occurred." },"innererror":{ "message":"the required anti-forgery cookie \"__requestverificationtoken\" not present.","type":"system.web.mvc.httpantiforgeryexception","stacktrace":" @ system.web.helpers.antixsrf.tokenvalidator.validatetokens(httpcontextbase httpcontext, iidentity identity, antiforgerytoken sessiontoken, antiforgerytoken fieldtoken)\r\n @ system.web.helpers.antixsrf.antiforgeryworker.validate(httpcontextbase httpcontext, string cookietoken, string formtoken)\r\n @ system.web.helpers.antiforgery.validate()... bla bla bla" } } } --changesetresponse_aa3c98d6-a059-454d-9477-00e0b9497278-- --batchresponse_efc71196-045b-474c-bfe3-e21e20ae64b0--
@update - stopgap we've implemented process when request received serverside (using custom odatabatchhandler, overriding validaterequest() method), grab tokens parent request , store in cache. when validate request within controllers, first check see if property in request (which inner request of batch once controller gets request, batch has been split multiple requests) , if not, see if in cache, looking @ parent request. it's important remove value cache, in our (processbatchasync() within custom odatabatchhandler) otherwise you're introducing security hole keeping around more 1 request.
good catch!
the problem in createchangerequests
method (line 15380) doesn't support custom tweaking of inner request.
we'll discuss internally find way address formally. short term can't think of work-around other create custom copy of odata dataservice adapter change fnc. yuck.
what's timeframe?
Comments
Post a Comment