Ejemplo n.º 1
0
        private async Task <HttpResponseMessage> SendAuthenticated(HttpRequestMessage request, CancellationToken cancellationToken, bool useNtlm = true)
        {
            request.Headers.Accept.Clear();
            request.Headers.Add("Accept", "*/*");
            //request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));

            var ntlm = new Ntlm(NetworkCredential);

            request.Headers.Authorization = CreateAuthenticationHeaderValue(ntlm.CreateNegotiateMessage(spnego: !useNtlm));

            Console.WriteLine(request);
            var response = await base.SendAsync(request, cancellationToken);

            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                foreach (AuthenticationHeaderValue header in response.Headers.WwwAuthenticate)
                {
                    string blob = ntlm.ProcessChallenge(header);
                    if (!string.IsNullOrEmpty(blob))
                    {
                        request = new HttpRequestMessage(HttpMethod.Get, request.RequestUri);
                        request.Headers.Clear();
                        request.Headers.Add("Accept", "*/*");
                        request.Headers.Authorization = CreateAuthenticationHeaderValue(blob);

                        Console.WriteLine(request);

                        response = await base.SendAsync(request, cancellationToken);
                    }
                }
            }

            Console.WriteLine(response);
            return(response);
        }
Ejemplo n.º 2
0
        private async Task <HttpResponseMessage> SendAuthenticated(HttpRequestMessage request, CancellationToken cancellationToken, bool useNtlm = true)
        {
            // We would rather not create a new HttpClient for this of course
            using var client = new HttpClient(InnerHandler);

            request = await request.CloneAsync();

            var ntlm = new Ntlm(NetworkCredential);

            request.Headers.Authorization = CreateAuthenticationHeaderValue(ntlm.CreateNegotiateMessage(spnego: !useNtlm));

            Console.WriteLine(request);

            // var response = await base.SendAsync(request, cancellationToken);     TODO we would like to do this but doesn't work ?!?!?
            var response = await client.SendAsync(request, cancellationToken);

            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                foreach (AuthenticationHeaderValue header in response.Headers.WwwAuthenticate)
                {
                    string blob = ntlm.ProcessChallenge(header);
                    if (!string.IsNullOrEmpty(blob))
                    {
                        request = await request.CloneAsync();

                        request.Headers.Authorization = CreateAuthenticationHeaderValue(blob);

                        Console.WriteLine(request);
                        // response = await base.SendAsync(request, cancellationToken); // TODO we would like to do this
                        response = await client.SendAsync(request, cancellationToken);
                    }
                }
            }

            Console.WriteLine(response);
            return(response);
        }