0

I am trying to call a webapi from C#, the webapi is hosted in IIS and requires login when you access it.

I don't know how to call the webapi with current login credential, and if I don't pass anything, the result will be the HTML for the login page.

using (var httpClient = new HttpClient())
{
    var response = httpClient.GetAsync("url").Result;                    
    var result = response.Content.ReadAsStringAsync().Result;
}

I remember there is a way to pass in the current login credential, but i can't seem to find it.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Zoe Yang
  • 1
  • 1
  • BTW, the twice calling of `.Result` on those async methods may lead to errors. Better use [AsyncBridge](https://github.com/tejacques/AsyncBridge) instead. – Uwe Keim Feb 15 '18 at 16:24
  • 2
    Possible duplicate of [HttpClient.GetAsync with network credentials](https://stackoverflow.com/questions/10292730/httpclient-getasync-with-network-credentials) – JuanR Feb 15 '18 at 16:25
  • What authentication method is the api? – penleychan Feb 15 '18 at 16:25
  • What is the controller action, that You try to call? Is it gest / post? Does it have any parameters? – zchpit Feb 15 '18 at 16:25
  • You should figure out more details on how the protection works, i.e. do you need to send a cookie, a token, etc, and how do you obtain them. There is no "common way" of providing login. – Ilya Chernomordik Feb 15 '18 at 16:29
  • It's a GET method, and there is no Authentication for the API other than it's hosted in a site that requires login – Zoe Yang Feb 15 '18 at 16:29

0 Answers0