Когда ReturnUrl -страница назначения- имеет атрибут Authorize и выполняется GetAuthenticationStateAsync(), protectedLocalStorage становится нулевым после NavigationManager.NavigateTo() выполняется.
Это соответствующий код для страницы входа
Код: Выделить всё
await protectedLocalStorage.SetAsync("authToken", token);
NavigationManager.NavigateTo(ReturnUrl ?? "");
Код: Выделить всё
ProtectedBrowserStorageResult result;
try
{
result = await protectedLocalStorage.GetAsync("authToken"); //protectedLocalStorage is null after the redirect
}
catch
{
result = new();
}
var anonymous = new ClaimsPrincipal(new ClaimsIdentity());
if (!result.Success) //Since protectedLocalStorage is null I get redirected back to login page.
{
return new AuthenticationState(anonymous);
}
Код: Выделить всё
@code {
[CascadingParameter] private HttpContext HttpContext { get; set; } = default!;
private IComponentRenderMode? RenderModeForPage => HttpContext.Request.Path.StartsWithSegments("/Account") ? null : new InteractiveServerRenderMode(prerender: false);
}
Похоже, что функция NavigateTo() вызывает GetAuthenticationStateAsync() слишком рано в жизненном цикле Blazor, protectedLocalStorage еще не готов.
Как убедиться, что protectedLocalStorage готов, когда я выполняю перенаправление?
Подробнее здесь: https://stackoverflow.com/questions/792 ... er-login-u
Мобильная версия