Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Blazor Client-Side consuming Web API with Windows Authentication

Currently I've got an application running with and angular client, consuming a Web API with Windows Authentication.

Now I'm looking into replacing this front end with Blazor (client-side), however I'm facing some challenges when it comes to authentication.

In angular I just set withCredentials to true in order to submit the required information.

The code below works as intended using Blazor server-side, but since I want to use Blazor client-side it's not an option and doesn't help me much.


    IEnumerable<SearchView> searchResults;
    int NumberOfItems;

    protected override async Task OnInitAsync()
    {
        using (var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true }))
        {
            var result = await client.GetJsonAsync<Response<SearchView>>("http://localhost:80/search");
            NumberOfItems = result.TotalItemCount;
            searchResults = result.Items;
        }
    }
}

The above code throws an "PlatformNotsupportedException".

WASM: System.PlatformNotSupportedException: System.Net.Http.HttpClientHandler is not supported on the current platform. WASM: at System.Net.Http.HttpClientHandler.set_UseDefaultCredentials (System.Boolean value) <0x1d63160 + 0x0000c> in <4399d2484a2a46159ade8054ed94c78e>:0

Clearly the code provided is not supported using Blazor client-side, but if there are any alternative ways to achieve what I want to, any pointers and help would be appreciated.

like image 605
karl Avatar asked Dec 31 '25 03:12

karl


2 Answers

I've just hit the same problem and couldn't get it working with HttpClient, but I did manage it with a HttpRequestMessage:

string APIURL = "https://localhost:44390/api/models";

// 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 Http.SendAsync(request);
models = await httpResponse.Content.ReadFromJsonAsync<myModel[]>();
like image 161
ca8msm Avatar answered Jan 02 '26 17:01

ca8msm


This is not (yet) possible. Blazor client-side runs on the Mono runtime of the .net framework which does not support Windows Authentication.

Your best option is to implement a token based auth (JWT for instance) and use ADFS.

like image 32
Flores Avatar answered Jan 02 '26 17:01

Flores



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!