Web API call from MVC (cookie check)

I have Web Api and Mvc 5 in the same project. This Web Api is protected by a carrier token (but I commented on the SuppressDefaultHostAuthentication line, so I can access the api from the browser when I authenticate with the mvc cookie)

Now I'm trying to access the api from the mvc controller without sending a token, is this possible with Disabling SuppressDefaultHostAuthentication?

Tried this without success (error 401):

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

using (var client = new HttpClient(handler))
{
     client.BaseAddress = new Uri("http://localhost:11374/");
     client.DefaultRequestHeaders.Accept.Clear();
     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

     var response = client.GetAsync("api/MyApi").Result;
     if (response.IsSuccessStatusCode)
     {  }
}      

If this is not possible, what is the best way to deal with this problem?

+4
source share
2 answers

WebApi REST, , , , . , WebApi API, REST, , cookie, .. API , . , , , . . -, -.

+3

WebAPI MVC , HTTP - :)

WebAPI MVC. , .

/ , WebAPI, HTTP .

+2

All Articles