1

I have created a WebAPI application on my local machine using Windows Authentication.

I have also created an MVC 5 application and I am trying to connect to my WebAPI with the following code:

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("http://localhost:52613/api/acmeco/");
    var result = client.GetAsync("assignees/get").Result;
    string resultContent = result.Content.ReadAsStringAsync().Result;

    JavaScriptSerializer json_serializer = new JavaScriptSerializer();
    assignees = json_serializer.DeserializeObject(resultContent);
}

The problem is: Everytime I try to connect, either via the MVC 5 app, or via Fiddler, I get a 401 Unauthorized error.

I have tried a variety of solutions to fix this issue, including but not limited to the following:

http://support.microsoft.com/kb/896861/en-us

Does anyone know how I can call my local WebAPI from my local MVC 5 app?

Edit: I have also tried adding CORS (cross-origin scripting) to the WebAPI like so, but there seems no effect on the 401 error:

[EnableCors(origins: "*", headers: "*", methods: "*")]
public class AssigneesController : ApiController
{
    public Assignees AssigneeRepo = new Assignees();

    // GET api/values
    public IEnumerable<Assignee> Get(string @namespace)
    {
        return AssigneeRepo.GetAssigneesForTenant(@namespace);
    }
}

However, I can access localhost:52613/api/acmeco/assignees/get directly from my browser.

user1477388
  • 20,790
  • 32
  • 144
  • 264
  • HTTP 401 means you are not authorised. Does the WebApi have authentication on it? – DavidG Jul 31 '14 at 14:53
  • @DavidG Yes it uses Windows Authentication. I can call my WebAPI from the browser directly and it works. But, calling from Fiddler or a local MVC 5 app, it gives me a 401. – user1477388 Jul 31 '14 at 14:54
  • 1
    Have you tried passing in the windows creds? Check this - http://stackoverflow.com/questions/11608843/error-401-in-c-sharp – Gabbar Jul 31 '14 at 15:12
  • @user1477388 Your browser picks up the Windows account it's running under but when you create HttpClient, you need to pass that in manually. If the link from Gabbar doesn't work, there's plenty of other sites out there that should tell you how. – DavidG Jul 31 '14 at 15:18
  • @Gabbar Thanks Gabbar. The link wasn't the exact answer, I had to modify it a bit but basically that was it. – user1477388 Jul 31 '14 at 15:23

2 Answers2

3

you can try:

        HttpClientHandler handler = new HttpClientHandler()
        {
            UseDefaultCredentials = true
        };

    using(HttpClient client = new HttpClient(handler))
   {     
      client.BaseAddress = new Uri("http://localhost:52613/api/acmeco/");
      var result = client.GetAsync("assignees/get").Result;
      string resultContent = result.Content.ReadAsStringAsync().Result;

      JavaScriptSerializer json_serializer = new JavaScriptSerializer();
      assignees = json_serializer.DeserializeObject(resultContent);
  }
SoftSan
  • 2,482
  • 3
  • 23
  • 54
1

As @Gabbar's link above hints at, I need to use NetworkCredentials. I implemented this as follows and it works:

public ActionResult Index()
{
    var assignees = new object();

    using (var handler = new HttpClientHandler()) 
    {
        handler.Credentials = new System.Net.NetworkCredential(@"DOMAIN\USERNAME", "PASSWORD");
        using (var client = new HttpClient(handler))
        {
            client.BaseAddress = new Uri("http://localhost:52613/api/acmeco/");
            var result = client.GetAsync("assignees/get").Result;
            string resultContent = result.Content.ReadAsStringAsync().Result;

            JavaScriptSerializer json_serializer = new JavaScriptSerializer();
            assignees = json_serializer.DeserializeObject(resultContent);
        }
    }
    return View(assignees);
}
user1477388
  • 20,790
  • 32
  • 144
  • 264