我有一个带有侧边栏的主布局,对于授权和非授权用户来说是不同的。我要更新的地方看起来像那样
<AuthorizeView>
<Authorized>
// Personal information matches in this component (it's just one more div this some code in it)
<UserInfo />
</Authorized>
<NotAuthorized>
<div class="sidebar-unathorized">
<span>
To get all privileges, <a href="/register"><strong>register</strong></a> or <a href="/login"><strong>login</strong></a> please
</span>
</div>
</NotAuthorized>
</AuthorizeView>
Run Code Online (Sandbox Code Playgroud)
用户通过授权后,我希望他看到他的个人信息,所以在我的登录方法中,我做了一些类似的事情
public async void HandleValidSubmit()
{
...
((CustomAuthenticationStateProvider)authenticationStateProvider).AuthenticateUser(authorizedUser);
navigationManager.NavigateTo("/");
//here I want to update the layout
...
return;
}
Run Code Online (Sandbox Code Playgroud)
并在我的 CustomAuthenticationStateProvider 设置当前用户后
NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(user)));
,我希望这足以让所有基于授权的组件进行更新。但事实并非如此。我尝试了 StateHasChanged() 方法,但可以理解的是它不会像那样工作,因为它只是更新触发它的组件。但是,如果您在登录后手动重新加载页面,一切都会好起来的。任何想法如何从代码更新布局?
我正在实现一个自定义AuthenticationStateProvider并使用来自 mainLayout 中用户声明的信息。据我了解,执行NotifyAuthenticationStateChanged方法后应该自己重新渲染所有使用<AuthorizeView>etc的组件,但事实并非如此。此外,我为 mainLayout 实现了我自己的重新加载器,并StateHasChanged在用户登录后使用它重新加载它。但出于某种原因,它仍然认为没有人授权并在<NotAuthorized>块中呈现代码块。但是,如果我手动重新加载页面,GetAuthenticationStateAsync则执行方法并在<Authorized>呈现内部代码块之后。我做错了还是错误?我的CustomAuthenticationStateProvider代码:
public class CustomAuthenticationStateProvider : AuthenticationStateProvider
{
private readonly ISessionStorageService _sessionStorage;
public CustomAuthenticationStateProvider(ISessionStorageService sessionStorage)
{
_sessionStorage = sessionStorage;
}
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
var userModel = await _sessionStorage.GetItemAsync<AuthorizedModel>("userModel");
var identity = new ClaimsIdentity();
if (userModel != null)
{
identity = new ClaimsIdentity( new []
{
//Some my claims
...
}, "api");
}
else
{
identity = new ClaimsIdentity(); …Run Code Online (Sandbox Code Playgroud)