簡體   English   中英

將 Blazor 客戶端消費 Web API 與 Windows 身份驗證結合使用

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

這(還)不可能。 Blazor 客戶端在不支持 Windows 身份驗證的 .net 框架的 Mono 運行時上運行。

您最好的選擇是實施基於令牌的身份驗證(例如 JWT)並使用 ADFS。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM