Posts Fetching data from an API
Post
Cancel

Fetching data from an API

Fetching Data from an API from Blazor WASM

Blazor WASM Windows Authentication

I did a lot of research and it took me a while until I found the proper way to call the Web API. Normally, without Windows authentication, you could just use one line of code to call the Web API

1
    var currentUser = await _httpClient.GetFromJsonAsync<UserData>($"auth/currentuser");

Here is how to do it with Windows Authentication

1
2
3
4
5
6
7
8
9
10
string APIURL = baseUri + "auth/currentuser";

// create request object and pass windows authentication credentials
var request = new HttpRequestMessage(HttpMEthod.Get, APIURL);
request.SetBrowserRequestCredentials(BrowserRequestCredentials.Include);

// send the request and convert the results to a list
var HttpResponse = await _httpClient.SendAsync(request);
var _currentUser = await httpResponse.Content.ReadFromJsonAsync<CurrentUser>();
return _currentUser;

I wish Microsoft had included the above example in its documentation or created an extension into httpClient.

This post is licensed under CC BY 4.0 by the author.