xszaboj
xszaboj

Reputation: 1085

HttpClient with NetworkCredential returns 401 for .net core 200 for .net framework

I have this code:

using System.Net;
using System.Net.Http;
    class Program
    {
        static void Main(string[] args)
        {
            NetworkCredential credential = new NetworkCredential("user", "password", "domain");

            using (HttpClientHandler handler = new HttpClientHandler() { Credentials = credential })
            {
                using (HttpClient client = new HttpClient(handler))
                {
                    string authUrl = "https://example.net";
                    var authresponse = client.GetAsync(authUrl).Result;
                }
            }
        }
    }

(url and credentials are anonymized)

In .net framework console I get response 200.

In .net core 1.1 I get response 401.

I guess it is difference in implementation between .net core and .net framework. Any ideas how to solve this? Or where to report this bug/feature?

Thanks

Upvotes: 1

Views: 2667

Answers (2)

Madison Haynie
Madison Haynie

Reputation: 488

I believe you should be able to use a CredentialCache explicitly specifying that you're using NTML scheme. Also apparently this should not be necessary in more recent .netcore versions

https://github.com/dotnet/corefx/issues/9533

Upvotes: 0

xszaboj
xszaboj

Reputation: 1085

In the end I solved it like this:

NetworkCredential credential = new NetworkCredential("user", "password", "domain");

using (WinHttpHandler handler = new WinHttpHandler() { ServerCredentials = credential})
{
    using (HttpClient client = new HttpClient(handler))
    {
        string authUrl = "https://example.net";
        var authresponse = client.GetAsync(authUrl).Result;
    }
}

Upvotes: 3

Related Questions