我在授权的情况下编写了这个基本的 Blazor Web 程序集应用程序。我使用
[AllowAnonymous]
和 [Authorize]
来允许/保护路线。但是由于某种原因,我不能授权自己打开一个页面。我真的很挣扎。
这是重定向后呈现的内容:
[Microsoft.AspNetCore.Authorization.DefaultAuthorizationService] Authorization failed. These requirements were not met:
DenyAnonymousAuthorizationRequirement: Requires an authenticated user.
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
@if (context.User.Identity is { IsAuthenticated: true })
{
<text>You are not authorized to access this resource.</text>
}
else
{
<LoginRedirect/>
}
</NotAuthorized>
<Authorizing>
<text>Please wait, we are authorizing you ...</text>
</Authorizing>
</AuthorizeRouteView>
<FocusOnNavigate RouteData="@routeData" Selector="h1"/>
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>
public override Task<AuthenticationState> GetAuthenticationStateAsync()
{
var identity = new ClaimsIdentity();
// ReSharper disable once InvertIf
if (IsLoggedIn())
{
var claims = new[]
{
new Claim(ClaimTypes.Name, _state.UserInfo!.Name),
new Claim(ClaimTypes.NameIdentifier, _state.UserInfo!.Name)
};
identity = new ClaimsIdentity(claims, "auth");
}
return Task.FromResult(new AuthenticationState(new ClaimsPrincipal(identity)));
}
尝试将身份验证类型设置为
“基本”
声明标识AuthenticationTypes需要是受支持的类型
identity = new ClaimsIdentity(claims, "Basic");
然后检查它是否通过了身份验证
var isAuthenticated = identity.IsAuthenticated