2
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;

namespace ConsoleProgram
{
    public class Class1
    {
        private const string URL = "https://sun.domain.com/v1/service/token";
        static void Main(string[] args)
        {
            var handler = new HttpClientHandler();
            handler.Credentials = new System.Net.NetworkCredential("admin@client", "admin");
            HttpClient client = new HttpClient(handler);
            //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", "admin", "admin"))));
            //  client.BaseAddress = new Uri(URL);
            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));
            // List data response.
            HttpResponseMessage response = client.GetAsync(URL).Result;  // Blocking call!
            String res = response.ToString();
            Console.WriteLine(res);
        }
    }
}

I am getting unauthorized error even though I am passing the correct credentials. Any ideas?

I tried almost all the answers posted on StackOverflow.

6
  • 4
    Please put some effort into formatting your code - it's very hard to read with no indentation... Commented Aug 26, 2014 at 21:33
  • 1
    You've also given no background information, so it's very difficult to tell what you're trying to do, edit the question and give some more information about what your actually trying to accomplish in general. Commented Aug 26, 2014 at 21:43
  • Depends on what type of authentication "https://sun.domain.com/v1/service/token" supports. Commented Aug 26, 2014 at 21:49
  • Sorry for not properly indenting the code. Commented Aug 26, 2014 at 21:51
  • I am trying to consume a c# rest service using httpclient. I will look into the type of authentication the server is using now. Thanks. Commented Aug 26, 2014 at 21:52

1 Answer 1

10

For Basic Authentication, you need to send the credentials in an authorization header called "Basic" with base64-encoded "username:password" as the value. This should get the job done:

var headerVal = Convert.ToBase64String(Encoding.UTF8.GetBytes("admin@client:admin"));
var header = new AuthenticationHeaderValue("Basic", headerVal);
client.DefaultRequestHeaders.Authorization = header;
Sign up to request clarification or add additional context in comments.

3 Comments

Todd, I tried your method but it is the same unauthorized response again. Thank you.
Yeah I didn't even notice the commented-out line. Hmm. You tried without the HttpClientHandler, i.e. just new HttpClient(); ? Don't include that AND the auth header. And did you try authenticating at that URL from a browser? I really suspect your code is not the problem.
Todd, there was a problem with the url itself. Thanks. I figured it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.