1
votes

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.

2
Why do you want to replace a working client-side solution which has good compatibility with WASM, which is basically still an experimental tech with less browser support? What are you hoping to gain, exactly? P.s. on which browser(s) are you seeing the above error? - ADyson
What happens if you don't use the HttpClientHandler? - mjwills

2 Answers

1
votes

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.

0
votes

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[]>();