简体   繁体   English

将 Blazor 客户端消费 Web API 与 Windows 身份验证结合使用

[英]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.目前,我有一个使用 angular 客户端运行的应用程序,使用带有 Windows 身份验证的 Web API。

Now I'm looking into replacing this front end with Blazor (client-side), however I'm facing some challenges when it comes to authentication.现在我正在考虑用 Blazor(客户端)替换这个前端,但是在身份验证方面我面临一些挑战。

In angular I just set withCredentials to true in order to submit the required information.在 angular 中,我只是将 withCredentials 设置为 true 以提交所需的信息。

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.下面的代码使用 Blazor 服务器端按预期工作,但由于我想使用 Blazor 客户端,因此它不是一个选项,对我帮助不大。


    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".上面的代码抛出一个“PlatformNotsupportedException”。

WASM: System.PlatformNotSupportedException: System.Net.Http.HttpClientHandler is not supported on the current platform. WASM:System.PlatformNotSupportedException:当前平台不支持 System.Net.Http.HttpClientHandler。 WASM: at System.Net.Http.HttpClientHandler.set_UseDefaultCredentials (System.Boolean value) <0x1d63160 + 0x0000c> in <4399d2484a2a46159ade8054ed94c78e>:0 WASM:在 System.Net.Http.HttpClientHandler.set_UseDefaultCredentials(System.Boolean 值)<0x1d63160 + 0x0000c> 在 <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.显然,使用 Blazor 客户端不支持提供的代码,但如果有任何其他方法可以实现我想要的目标,任何指针和帮助将不胜感激。

I've just hit the same problem and couldn't get it working with HttpClient, but I did manage it with a HttpRequestMessage:我刚刚遇到了同样的问题,无法与 HttpClient 一起使用,但我确实使用 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. Blazor 客户端在不支持 Windows 身份验证的 .net 框架的 Mono 运行时上运行。

Your best option is to implement a token based auth (JWT for instance) and use ADFS.您最好的选择是实施基于令牌的身份验证(例如 JWT)并使用 ADFS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 客户端认证 Blazor web app - Authentication in client-side Blazor web app 客户端 Blazor 对 GetRequest 使用 SendAsync 对 API 请求使用 CORS - Client-side Blazor using SendAsync for a GetRequest with CORS for an API request Blazor 客户端和 WCF - Blazor client-side and WCF 使用Web API的客户端Web应用程序,如何根据服务器端Web API的期望填充选择框字段值? - Client-side web app consuming Web API, how to populate select box field values based on expectations of server-side Web API? Blazor(客户端)StateHasChanged() 不更新页面 - Blazor (Client-side) StateHasChanged() not updating page 为什么客户端 Blazor 使用 .NET Standard 2.0 以及如何将 .NET Core 3.0 与 Blazor 一起使用? - Why is client-side Blazor using .NET Standard 2.0 and how to use .NET Core 3.0 with Blazor? Java Applet消耗客户端dotnet框架可重新分发 - Java Applet consuming client-side dotnet framework redistributable 如何在 Blazor 中执行客户端 UI 事件 - How to do client-side UI events in Blazor 如何使用 blazor 客户端应用程序在服务器上创建新文件? - How to create a new file on the server with blazor client-side application? 在 blazor 中使用带有身份验证的 WebAPI - Consuming WebAPI in blazor with authentication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM