This is a migrated thread and some comments may be shown as answers.

Odata not working with custom authorization header

7 Answers 1098 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Zentraler
Top achievements
Rank 1
Zentraler asked on 22 Aug 2014, 01:31 PM
Hi,

I have a serious issue with odata requests and an authentication header I have to set - it just is not working! Here is the transport part of my datasource definition (done inside the grid configuration:

transport: {
                  type: "odata",
                   read: {
                       url: "http://........",
                       beforeSend: function (xhr) {
                           var auth = 'Bearer ' + token;
                           xhr.setRequestHeader('Authorization', auth);
                       }
                   },
 
               },

If I remove the line saying "type = odata", then the header is correctly set (of course, the request is not delivering useful data then...). If I add the line, the "beforeSend" part is executed, but the header is not set in the request.

Could this be a bug?

Best regards,
Gheorghe

7 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 25 Aug 2014, 03:54 PM
Hi Gheorghe,

As you may know when odata type is set, by default the request will be executed via JSONP. Therefore, due to the way JSONP works there is not way to set the authorization headers. However, if the service you are trying to access is in the same domain as the application or the CORS are enabled you should set the read request dataType option to json, which will then allow setting the authorization headers.

transport: {
    type: "odata",
     read: {
         url: "http://........",
         dataType: "json",
         beforeSend: function (xhr) {
             var auth = 'Bearer ' + token;
             xhr.setRequestHeader('Authorization', auth);
         }
     },
 
 },


Regards,
Rosen
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Ingo
Top achievements
Rank 1
answered on 17 Feb 2015, 01:12 PM
Hi Rosen,

I have exactly the same problem. My DataSource is initialized like this:
kendo.data.DataSource({
        type: "odata-v4",
        transport: {
          read: {
            url: dataUrl,
            dataType: 'json',
            beforeSend: function (req) {
              var auth = self.options.authenticationType + " " + self.options.authenticationToken;
              req.setRequestHeader('Authorization', auth);
            }
          }
        }
      });

The authorization header is not set however. Is this due to odata-v4?
0
Rosen
Telerik team
answered on 18 Feb 2015, 08:16 AM
Hi Ingo,

The code you have pasted should assign the authorization headers as expected. Please provide a small runnable sample in which the issue can be observed locally.

Regards,
Rosen
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Bill
Top achievements
Rank 1
answered on 12 Feb 2016, 06:40 PM
I also have the same issue, my request headers are not being set. 
 
type: "odata",
            transport: {
                read: {
                    url: "https://....",
                    dataType: 'json',
                    beforeSend: function (req) {
                        req.setRequestHeader('Authorization', auth);
                        req.setRequestHeader('Access-Control-Allow-Origin', '*')
                    }
                },

 

 

0
Rosen
Telerik team
answered on 15 Feb 2016, 02:25 PM

Hello Bill,

 

Maybe you could provide  a small runnable sample which demonstrate the issue you are experiencing. Also you may test your service and AJAX request configuration by making the same request but using just jQuery.ajax. As you know, the DataSource remote transport uses jQuery.ajax to make AJAX requests, thus same configuration should work for both.

 

Regards,
Rosen
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Jason
Top achievements
Rank 1
answered on 03 May 2016, 05:56 PM
Is anyone going to answer this question.  I had to come up with a .NET hack to get this work.  If someone provides you with an example they would have to set up all the security piece as well.  That is unacceptable for this type of issue.  
0
Rosen
Telerik team
answered on 04 May 2016, 09:09 AM

Hello Jason,

As mentioned in the initial message using the beforeSend is the way to modify the request header. I have mentioned that the DataSource is not directly involved in send the headers as the configuration is passed to the underlying jQuery ajax call behind the scenes. Thus, the same approach applicable with jQuery (and discussed here for example) can be applied to the DataSource too. This is why I have suggested that in order to locally troubleshoot the problem a jQuery ajax call can be used leaving aside the DataSource. Then the same configuration can be used with the DataSource. If it cannot be get to work with jQuery ajax, then the problem may be somewhere else, for example the configuration of the server. This is the steps to self troubleshoot the issue, otherwise a sample project (leaving the server-side of the things out as much as possible as it is not directly related to Kendo UI) which we can look  at locally will be required.

Regards,
Rosen
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Data Source
Asked by
Zentraler
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Ingo
Top achievements
Rank 1
Bill
Top achievements
Rank 1
Jason
Top achievements
Rank 1
Share this question
or