Eaglefly
Eaglefly

Reputation: 3

c#: use current credentials for api that uses basic authentication

I have an c# .net 5 app running as a scheduled task on a server. The scheduled task runs with a user, that has privileges to access an api. The API uses basic authentication, but I can't figure out how to use basic auth, without sending the encoded user/pwd in the AuthenticationHeader. How is it possible to use the default credentials? I tried this out, but get access denied

   HttpClientHandler handler = new();
   handler.UseDefaultCredentials = true;
   HttpClient client = new HttpClient(handler);
   client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic");
   var request = new HttpRequestMessage(HttpMethod.Get, "some uri"); 

Upvotes: 0

Views: 914

Answers (1)

J.Salas
J.Salas

Reputation: 1363

if API uses Basic Auth you need to send the usr/pwd over, no workaround

The Basic authentication scheme is a widely used, industry-standard method for collecting user name and password information. Basic authentication transmits user names and passwords across the network in an unencrypted form. You can use SSL encryption in combination with Basic authentication to help secure user account information transmitted across the Internet or a corporate network.

https://learn.microsoft.com/en-us/iis/configuration/system.webserver/security/authentication/basicauthentication

You need to change the API auth method to achieve what you want (read this on the same documentation)

Upvotes: 1

Related Questions