简体   繁体   中英

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM