httpclient unauthorized error 401

1 Answer 923 Views
Grid Security
Alberto
Top achievements
Rank 1
Alberto asked on 27 Jan 2023, 02:34 PM

My application is redirected from https:\\application.domain.com to http:\\servername.domain.net

I'm using windows authentication with [authorize] control in webAPI.

Whether I call my webAPI to populate a datasource it works:

    .DataSource(source => source
        .Custom()
        .Transport(transport =>
        {
            transport.Read(new
            {
                url = $"{WebApiUrl}",
                xhrFields = new { withCredentials = true },
                DataType = "json",
                Data = "forgeryToken"
            });
        })

Instead if I use the HTTPCLIENT class it seems that the request loses the credentials and it returns an unauthorize error 401

services.AddHttpClient<Class>()
                .ConfigurePrimaryHttpMessageHandler(handler =>
                   new HttpClientHandler()
                   {
                       AutomaticDecompression = System.Net.DecompressionMethods.GZip,
                       UseDefaultCredentials = true,
                   });

Whether I don't use the redirection and I call directly http:\\servername.domain.net it works.

How can I use HTTPCLIENT class to mantain the credential?

I know it's not related to telerik, but I don't understand why with telerik it works and I hope that someone can help me.

Thank you

1 Answer, 1 is accepted

Sort by
0
Stoyan
Telerik team
answered on 01 Feb 2023, 01:19 PM

Hi Alberto,

The difference in these configurations is that AddHttpClient method is a service collection extension and is applied to the server-side.

The DataSource's properties are ultimately serialized to the client-side and there the Web API's withCredentials method is used. Then Read requests sent to the specified Web API Url include credentials.

To send requests with included credentials utilize the method of the Web API on the client-side.

I hope the information above is useful.

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
Grid Security
Asked by
Alberto
Top achievements
Rank 1
Answers by
Stoyan
Telerik team
Share this question
or