【发布时间】:2021-05-23 05:54:53
【问题描述】:
我一直在使用 .net 5.0 玩 blazor。我遇到了一个简单但烦人的问题:我正在尝试从 API 加载数据,但对服务器 blazor 的每次调用都希望用户登录。
我明白如果我们从 API 控制器中取出 [Authorize] 标记并且在 razor 页面中没有 @attribute [Authorize],那就是这样。但我很难理解为什么我的 API 调用仍然需要 protected override async Task OnInitializedAsync() 中的 AccessToken 错误消息并没有说明原因。
Microsoft.AspNetCore.Components.WebAssembly.Authentication.AccessTokenNotAvailableException: ''
at Microsoft.AspNetCore.Components.WebAssembly.Authentication.AuthorizationMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.SendAsyncCore(HttpRequestMessage request, HttpCompletionOption completionOption, Boolean async, Boolean emitTelemetryStartStop, CancellationToken cancellationToken)
我还没有碰过这个应用程序。 Web程序集的cs文件..
谁能帮帮我。
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
@if (!context.User.Identity.IsAuthenticated)
{
<RedirectToLogin />
}
else
{
<p>You are not authorized to access this resource.</p>
}
</NotAuthorized>
<Authorizing>
<h4>Authentication in progress...</h4>
</Authorizing>
</AuthorizeRouteView>
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>
【问题讨论】:
-
能否将您的应用上传到 github...
标签: asp.net-core blazor forms-authentication blazor-webassembly blazor-client-side