0

There is a web api that has SSO enabled. When I put the url in my webbrowser I don't have to specify a password and it returns the data. How can I do this same thing with C#? When I do it with the WebClient DownloadString() function I get an error of "unauthorized" back.

var client = new System.Net.WebClient();

var context = client.DownloadString(@"http://api_site.example.com/data?username=jdoe&report=test");
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
user441521
  • 6,942
  • 23
  • 88
  • 160
  • 1
    Your browser is probably automatically passing your Windows credentials, while the WebClient is not. – Camilo Terevinto Jun 15 '16 at 16:18
  • Any way I can get the WebClient to do the same? – user441521 Jun 15 '16 at 17:09
  • Yes. Add the credentials manually to the property `Credentials` on the `WebClient` instance. – Igor Jun 15 '16 at 17:14
  • I'm hoping for more of an SSO approach so the client doesn't require this. That pointed me in the right direction though. Seems if you set UseDefaultCredentials to true it uses the AD id of the person logged into the Windows box. Thx. – user441521 Jun 15 '16 at 17:17
  • the last comment on the accepted answer of this http://stackoverflow.com/questions/1680718/domain-credentials-for-a-webclient-class-dont-work fits here: `client.Credentials = CredentialCache.DefaultNetworkCredentials.GetCredential(uri, "NTLM");` – Cee McSharpface Jun 15 '16 at 17:29

0 Answers0