【发布时间】:2020-11-16 13:47:24
【问题描述】:
我有一个带有很多 html 标记的 Blazor(版本 net5.0)组件,这里是其中的一部分:
<a class="@(IsAuthenticated?"":"hide")" href="#">My link for logged in users</a>
这是我的 c# 代码(MyComponent.Razor):
[CascadingParameter] private Task<AuthenticationState> authenticationState { get; set; }
private AuthenticationState auth;
public bool IsAuthenticated { get; set; }
protected async override Task OnInitializedAsync()
{
auth = await authenticationState;
var user = auth.User.Identity.IsAuthenticated;
await base.OnInitializedAsync();
}
在我的登录组件中成功登录后我调用StateHasChanged();
void Login()
{
...
StateHasChanged();
}
但登录后不会应用对 MyComponent 的任何更改,除非我刷新页面以便组件重新呈现自身。
注意:我不想使用AuthorizeView,因为正如我所提到的,MyComponent 中有很多标记和组件,我不想使用对于我想根据用户身份验证更改其行为的每种样式或元素,都有一个 AuthorizeView。
更新:我的App.razor 组件中有以下代码:
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(Layout)" >
<Authorizing>
<text> my Custom Authotizing in app.razor ...</text>
</Authorizing>
<NotAuthorized>
<text> my Custom NOT Authotized in app.razor ...</text>
</NotAuthorized>
</AuthorizeRouteView>
</Found>
<NotFound>
<CascadingAuthenticationState>
<LayoutView Layout="@typeof(Layout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</CascadingAuthenticationState>
</NotFound>
</Router>
【问题讨论】:
-
另一个不允许使用 Blazor 标签的例子。 OP 应该始终说明他使用的是什么风味的 Blazor
-
@enet 没有主机的独立 WebAssembly
-
@enet 我不使用 OIDC 或 IdentyServer4。我认为当用户验证(无论如何)组件时应该通知。
-
@enet 谢谢。我使用 Jwt 令牌身份验证。它工作正常。 AuthorizeView 也可以正常工作。
-
@enet 不,我没有解决问题,我只是说我的代码的其他部分没有问题
标签: c# asp.net-core authentication blazor blazor-webassembly