0

i have to send a authorized request to a web api. It need an Username and a Password. I tried different methods but it does not work, here is my code so far:

public async Task<IActionResult> salutAsync(string id)
{
        string Username = "xxxxxxxxxxxxx";
        string Password = "xxxxxxxxxxxxx";

        string baseUrl = "someWebSite + id;

        HttpClient client = new HttpClient();
        var headerFormat = "Username=\"{0}\", Password=\"{1}\"";
        var authHeader = string.Format(headerFormat,
                                Uri.EscapeDataString(Username),
                                Uri.EscapeDataString(Password)
            );
        client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", authHeader);
        using (HttpResponseMessage res = await client.GetAsync(baseUrl))
        using (HttpContent content = res.Content)
        {
            string data = await content.ReadAsStringAsync();
            if (data != null)
            {
                return Ok(data);
            }
            return BadRequest(data);
        }
    }

If i debug it, it says that my authHeader is "Username=\"xxxxxxxxxxxxx\", Password=\"xxxxxxxxxxxxx\"" and i dont know why it does not work

Any help would be awesome! Thank you so much!

Horatiuw
  • 179
  • 2
  • 2
  • 12

0 Answers0