提问人:karl 提问时间:6/11/2019 更新时间:12/2/2020 访问量:2944
将 Blazor 客户端使用 Web API 和 Windows 身份验证Using Blazor Client-Side using Web API with Windows Authentication
Using Blazor Client-Side consuming Web API with Windows Authentication
问:
目前,我有一个使用 angular 客户端运行的应用程序,使用带有 Windows 身份验证的 Web API。
现在,我正在考虑用 Blazor(客户端)替换此前端,但是在身份验证方面我面临一些挑战。
在angular中,我只是将withCredentials设置为true,以便提交所需的信息。
下面的代码使用 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;
}
}
}
上面的代码抛出“PlatformNotsupportedException”。
WASM:System.PlatformNotSupportedException:当前平台不支持 System.Net.Http.HttpClientHandler。 WASM:在 <4399d2484a2a46159ade8054ed94c78e>:0 中System.Net.Http.HttpClientHandler.set_UseDefaultCredentials(System.Boolean 值)<0x1d63160 + 0x0000c>
显然,Blazor 客户端不支持提供的代码,但如果有任何替代方法可以实现我想要的目标,任何指针和帮助将不胜感激。
答:
这(还)不可能。Blazor 客户端在不支持 Windows 身份验证的 .net Framework 的 Mono 运行时上运行。
最好的选择是实现基于令牌的身份验证(例如 JWT)并使用 ADFS。
我刚刚遇到了同样的问题,无法让它与 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[]>();
评论
HttpClientHandler