c# - 使用带有 Windows 身份验证的 Blazor 客户端使用 Web API

标签 c# client-side blazor

目前我有一个应用程序与角度客户端一起运行,使用带有 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:在 System.Net.Http.HttpClientHandler.set_UseDefaultCredentials(System.Boolean 值)<0x1d63160 + 0x0000c> 在 <4399d2484a2a46159ade8054ed94c78e>:0

很明显,所提供的代码不支持使用 Blazor 客户端,但如果有任何替代方法来实现我想要的,任何指示和帮助将不胜感激。

最佳答案

我刚刚遇到了同样的问题,无法使用 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[]>();

关于c# - 使用带有 Windows 身份验证的 Blazor 客户端使用 Web API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56542696/

相关文章:

.net - 在 Ubuntu 和 Certbot 上使用 SSL 的 Blazor

c# - 忽略 C# 中的接口(interface)方法实现

c# - .cs 文件存储在虚拟操作系统中的哪里?

c# - 使用 DotNetOpenAuth 设置带有子域标识符的 OpenID 提供程序

javascript - 如何使用 javascript 从远程网站上传图像?

javascript - 在客户端防止 XSS(纯 Javascript)

asp.net-core - 动态创建和销毁 Blazor 客户端组件?

c# - 在 C# 代码中创建元素时出现 WPF 绑定(bind)错误

php - Google ReCaptcha 显示在表单提交时带有图像和客户端验证

asp.net - Blazor WASM 请求已被 CORS 策略阻止