restsharp / RestSharp

Simple REST and HTTP API Client for .NET
https://restsharp.dev
Apache License 2.0
9.57k stars 2.34k forks source link

CredentialCache.DefaultCredentials and UseDefaultCredentials on .NET8 Core #2236

Open yvovonBerg opened 2 months ago

yvovonBerg commented 2 months ago

Describe the bug It appears that CredentialCache.DefaultCredentials is being used by RestSharp but it doesn't properly pass through the credentials for some reason. It should connect through Kerberos.

To Reproduce Does not work on .NET 8 Core:

using System.Net;
using RestSharp;

class Program
{
    static void Main(string[] args)
    {
        var options = new RestClientOptions("https://secure.server.example.com") 
        { 
            Credentials = CredentialCache.DefaultCredentials
        };

        RestClient Client = new RestClient(options);
        var request = new RestRequest("document/EXAMPLE-1");
        var response = Client.Execute(request);
        Console.WriteLine(response.ErrorMessage);
        Console.WriteLine(response.Content);
    }
}

But does work on .NETFramework v4.8

Expected behavior It should authenticate correctly.

Stack trace 401 response from the webserver.

Desktop (please complete the following information):

Additional context I've also tried to use the useDefaultCredentials flag and the workaround here: https://github.com/dotnet/runtime/issues/84545 of swapping the credentials and useDefaultCredentials arguments but that does not resolve the issue sadly.

Thank you!

alexeyzimarev commented 1 month ago

Have you tried setting UseDefaultCredentials to true?

yvovonBerg commented 1 month ago

Have you tried setting UseDefaultCredentials to true?

Yes, the same result sadly.

alexeyzimarev commented 1 month ago

RestSharp doesn't really do anything with those credentials apart from configuring the HTTP message handler with these parameters. I would suggest trying to use WinHttpMessageHandler as you are running on Windows. You can use RestClientOptions.ConfigureMessageHandler and configuring it explicitly.

yvovonBerg commented 1 month ago

Thanks, I tried with this as well:

            Client = new RestClient(new RestClientOptions(Url + Api)
            {
                UseDefaultCredentials = true,
                ConfigureMessageHandler = handler =>
                {
                    if (handler is HttpClientHandler clientHandler)
                    {
                        clientHandler.UseDefaultCredentials = true;
                    }

                    return handler;
                }
            });

Same result :(

alexeyzimarev commented 1 month ago

The code you tried doesn't do more than RestSharp does. I suggested using WinHttpHandler, but I don't know if it will work. If you can make it work with HttpClient, it will work with RestClient.