Код: Выделить всё
public class Constants
{
public static string JWTToken { get; set; } = "";
}
public class CustomAuthenticationStateProviderService : AuthenticationStateProvider
{
private readonly ClaimsPrincipal anonymous = new(new ClaimsIdentity());
private readonly ProtectedLocalStorage localStorage;
public CustomAuthenticationStateProviderService(ProtectedLocalStorage localStorage)
{
this.localStorage = localStorage;
}
public async override Task GetAuthenticationStateAsync()
{
try
{
var token = await localStorage.GetAsync("headerToken");
if (string.IsNullOrWhiteSpace(Constants.JWTToken))
return await Task.FromResult(new AuthenticationState(anonymous));
var claims = JWTService.DecryptToken(Constants.JWTToken);
if (claims == null) return await Task.FromResult(new AuthenticationState(anonymous));
var claimsPrincipal = SetClaimPrincipal(claims);
return await Task.FromResult(new AuthenticationState(claimsPrincipal));
}
catch (Exception ex)
{
return await Task.FromResult(new AuthenticationState(anonymous));
}
}
}
Код: Выделить всё
'JavaScript interop calls cannot be issued at this time. This is because the component is being statically rendered. When prerendering is enabled, JavaScript interop calls can only be performed during the OnAfterRenderAsync lifecycle method.'Я окружил локальное хранилище get с помощью try catch, который исправляет. Я нашел ответ, в котором говорится, что такое поведение вызвано builder.Services.AddCascadingAuthenticationState();, который у меня есть в program.cs
Код: Выделить всё
try
{
var token = await localStorage.GetAsync("headerToken");
}
catch (InvalidOperationException)
{
}
Код: Выделить всё
JavaScript interop calls cannot be issued at this time. This is because the circuit has disconnected and is being disposed.Подробнее здесь: https://stackoverflow.com/questions/780 ... calstorage
Мобильная версия