Search code examples
c#.nethttpwebrequest

HTTPWebRequest API Post fails login


I have an API that has SSO enabled on it. I am using HttpWebRequest to call the API to fetch data from it. It is a queuing website and I am fetching the message count in a specific queue.

Code is as below -

Uri rAPIURL = new Uri(ConfigurationManager.AppSettings["APIURL"]);
UriBuilder uriBuilder = new UriBuilder(rAPIURL);
uriBuilder.Path += string.Format(@"/api/queues/%2f/{0}", queuename);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriBuilder.Uri.ToString());
request.Credentials = new NetworkCredential(userName, password);

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    using (Stream stream = response.GetResponseStream())
    {
        using (StreamReader reader = new StreamReader(stream))
        {
            RespJSON = reader.ReadToEnd();
        }
    }
}

When I browse this API it first lands on a login page and after I provide the same credentials it shows the output.

The above code returns an html document, that i think is the login page (as mentioned below)-

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <title>Login</title>
  <base href="/ui/login/">  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="./assets/img/icons/evfavicon.png">
<link rel="stylesheet" href="styles.a00ae94a61c1e0311cdb.css"></head>

<body>
  <app-root></app-root>
<script src="runtime.c51bd5b1c616d9ffddc1.js" defer></script><script src="polyfills-es5.87acc165b580c818887f.js" nomodule defer></script><script src="polyfills.37f657d7ecbed38a02b2.js" defer></script><script src="main.41e579df33990ee1a128.js" defer></script></body>

</html>

I have also tried setting the default credentials in the request but it still gives the same response.

Any ideas on how to resolve this?


Solution

  • The default credentials were not added on the queuing website that were used in the earlier version. It was resolved by adding the default credentials and providing the required access level.