我一直在玩 blazor 和 .net 5.0。我遇到了一个简单但烦人的问题:我正在尝试从 API 加载数据,但对 Server blazor 的每次调用都希望用户登录。
我知道如果我们从 API 控制器中取出 [Authorize] 标签并且剃刀页面中没有 @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>
答案 0 :(得分:1)
我认为这是因为您有 CascadingAuthenticationState
封装了 Found
context 和 NotFound
,因为它的作用是将授权信息传递给其他组件。因此,将其包裹在所有组件中可以防止未经授权的用户访问。试试这个
<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>
<CascadingAuthenticationState>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
<CascadingAuthenticationState>
</NotFound>
</Router>
答案 1 :(得分:1)
我认为解决方案......应该先看看它。
它实际上是客户项目中的 App.CS 填写。 Http 端点在那里启动,默认情况下,代码生成将 BaseAddressAuthorizationMessageHandler 添加到 httpclient。
builder.Services.AddHttpClient("private", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
我的解决方法是使用命名客户端,一个用于私人,另一个用于公共
builder.Services.AddHttpClient("private", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
builder.Services.AddHttpClient("public", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress));
并在页面中注入 IHttpClientFactory
@inject IHttpClientFactory ClientFactory
并从工厂创建一个命名客户端。
var client = ClientFactory.CreateClient("public");
这奏效了。