DataSource Unauthorized error

1 Answer 69 Views
DropDownList Grid Security
Alberto
Top achievements
Rank 1
Alberto asked on 02 Jan 2023, 09:50 AM

In my webAPI i'm using the [Authorize] control.

When I call my webAPI to populate a grid or a dropdown with this code

.DataSource(source=>source.Custom()
        .Transport(transport=>transport.Read(read=>
        {
            read.Url($"{WebApiUrl}")
            .DataType("json").Data("forgeryToken");
        })
        )
        .PageSize(12)

only in localhost it returns this error <Failed to load resource: the server responded with a status of 401 (Unauthorized)> .

Instead if I use an ajax call in javascript it works

$.ajax({
        url: `${WebApiUrl}`,
        method: 'get',
        crossDomain: true,
        cache: false,
        xhrFields: {
            withCredentials: true,
        },
        success: function (data) {
            options.success(data)
        }
    })

I've searched in google but i didn't find anything that can help me. Do I have to pass the credentials somehow?

Thank you

1 Answer, 1 is accepted

Sort by
0
Stoyan
Telerik team
answered on 04 Jan 2023, 01:48 PM

Hi Alberto,

You can add the withCredentials attribute by using the Read transport's overload that uses an object:

.DataSource(dataSource => dataSource
                                    .Custom()
                                    .PageSize(12)
                                    .Transport(t =>
                                    {
                                        t.Read(new
                                        {
                                            url = Url.Action("Orders_Read", "Grid"),
                                            xhrFields = new { withCredentials = true }
                                        });
                                    })
                                 )
This approach was also suggested in the related forum thread here.

Please don't hesitate to address any additional questions on the topic should they occur.

Regards,
Stoyan
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
DropDownList Grid Security
Asked by
Alberto
Top achievements
Rank 1
Answers by
Stoyan
Telerik team
Share this question
or