Authorization 通过服务器端blazor的授权调用WebAPI

Authorization 通过服务器端blazor的授权调用WebAPI,authorization,blazor,server-side,webapi,Authorization,Blazor,Server Side,Webapi,抱歉,如果这是一个有点愚蠢的问题-我正试图让我的头周围的安全设置,并没有太多的经验在这方面。我已经尽可能多地阅读了,但是找不到一个明确的例子来说明我在做什么 我已经从visual studio模板创建了默认服务器端和wasm blazor项目,并共享了wasm项目,因此我可以根据Carl Franklin的文章重新使用客户端和服务器端: 一切正常 接下来,我重复一遍,但在创建时将“个人用户帐户”选项添加到两个项目中,将db字符串设置为共享标识数据库。但是,当我共享客户机代码并从服务器端blaz

抱歉,如果这是一个有点愚蠢的问题-我正试图让我的头周围的安全设置,并没有太多的经验在这方面。我已经尽可能多地阅读了,但是找不到一个明确的例子来说明我在做什么

我已经从visual studio模板创建了默认服务器端和wasm blazor项目,并共享了wasm项目,因此我可以根据Carl Franklin的文章重新使用客户端和服务器端:

一切正常

接下来,我重复一遍,但在创建时将“个人用户帐户”选项添加到两个项目中,将db字符串设置为共享标识数据库。但是,当我共享客户机代码并从服务器端blazor调用时,这两种方法都可以单独工作,webapi调用失败并出现“未经授权”的错误

总之,我成功地登录到服务器端blazor项目。当我试图调用webapi时,失败就发生了。webapi现在位于一个单独的项目(WASM项目)中,因此将在另一个域中运行(不要认为我遇到了cors问题)。当我试图调用webapi时,我得到了未经授权的错误。当我在WASM中运行时,一切正常

有人能告诉我需要采取什么步骤才能让它正常工作吗?剃须刀组件的完整代码如下

@page "/fetchdata"
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using BlazorWasm.Shared
@attribute [Authorize]
@inject HttpClient Http

<h1>Weather forecast</h1>

<p>This component demonstrates fetching data from the server.</p>

@if (forecasts == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <table class="table">
        <thead>
            <tr>
                <th>Date</th>
                <th>Temp. (C)</th>
                <th>Temp. (F)</th>
                <th>Summary</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var forecast in forecasts)
            {
                <tr>
                    <td>@forecast.Date.ToShortDateString()</td>
                    <td>@forecast.TemperatureC</td>
                    <td>@forecast.TemperatureF</td>
                    <td>@forecast.Summary</td>
                </tr>
            }
        </tbody>
    </table>
}

@code {
    private WeatherForecast[] forecasts;

    protected override async Task OnInitializedAsync()
    {
        try
        {
            string url = "https://localhost:44378/WeatherForecast";
            forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>(url);
        }
        catch (AccessTokenNotAvailableException exception)
        {
            exception.Redirect();
        }
        catch(HttpRequestException exception)
        {
            string msg = exception.Message;
        }
    }

}
@page”/fetchdata
@使用Microsoft.AspNetCore.Authorization
@使用Microsoft.AspNetCore.Components.WebAssembly.Authentication
@使用BlazorWasm.Shared
@属性[授权]
@注入HttpClient Http
天气预报
此组件演示如何从服务器获取数据

@如果(预测==null) { 加载

} 其他的 { 日期 温度(C) 温度(F) 总结 @foreach(预测中的var预测) { @forecast.Date.ToShortDateString()的 @预测温度 @预测温度 @预测.摘要 } } @代码{ 私人天气预报[]预测; 受保护的重写异步任务OnInitializedAsync() { 尝试 { 字符串url=”https://localhost:44378/WeatherForecast"; 预测=等待Http.GetFromJsonAsync(url); } 捕获(AccessTokenNotAvailableException异常) { 异常。重定向(); } 捕获(HttpRequestException异常) { 字符串msg=exception.Message; } } }