0

I have these two methods in my xamarin.form application. I have added b2c authentication to my app. now how can I change these methods to use token. sorry if this is very basic question but i am so junior. thanks

 private static async Task<string> SendGetRequest(string url)
    {
        var httpClient = new HttpClient();
        string responseString = string.Empty;
        var response = await httpClient.GetAsync(url);

        if (response.IsSuccessStatusCode)
        {
            responseString = await response.Content.ReadAsStringAsync();
        }
        else
        {
           ...
        }

        return responseString;
    }

    private static async Task SendPostRequest(string url, object payLoad)
    {
        try
        {
            var httpClient = new HttpClient();
            var stringContent = new StringContent(JsonConvert.SerializeObject(payLoad), Encoding.UTF8, "application/json");
            var response = await httpClient.PostAsync(url, stringContent).ConfigureAwait(false);

            if (!response.IsSuccessStatusCode)
            {
                ...
            }
        }
        catch (InvalidOperationException ex)
        {
            ...
        }
        catch (Exception ex)
        {
            ...
        }
    }
Mah
  • 69
  • 1
  • 7
  • 5
    Does this answer your question? [Setting Authorization Header of HttpClient](https://stackoverflow.com/questions/14627399/setting-authorization-header-of-httpclient) – Selim Yildiz Jan 10 '20 at 04:30

1 Answers1

0

Use Authorization header for authenticate:

private static async Task<string> SendGetRequest(string url)
    {
        var httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authorizationType, accessToken);
        string responseString = string.Empty;
        var response = await httpClient.GetAsync(url);

        if (response.IsSuccessStatusCode)
        {
            responseString = await response.Content.ReadAsStringAsync();
        }
        else
        {
           ...
        }

        return responseString;
    }

    private static async Task SendPostRequest(string url, object payLoad)
    {
        try
        {
            var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authorizationType, accessToken);
            var stringContent = new StringContent(JsonConvert.SerializeObject(payLoad), Encoding.UTF8, "application/json");
            var response = await httpClient.PostAsync(url, stringContent).ConfigureAwait(false);

            if (!response.IsSuccessStatusCode)
            {
                ...
            }
        }
        catch (InvalidOperationException ex)
        {
            ...
        }
        catch (Exception ex)
        {
            ...
        }
    }

Here authorizationType is like "Basic" or "Bearer" and accessToken is your token.

Srusti Thakkar
  • 1,835
  • 2
  • 22
  • 52