Проблема, с которой я сталкиваюсь, заключается в том, что значение _userId продолжает меняться при каждом обновлении, поэтому я Я не могу отслеживать состояние пользователя, используя то же самое.
Код: Выделить всё
protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
user = authState.User;
var _userId = user.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
}
Код: Выделить всё
public class ApplicationAuthenticationStateProvider : AuthenticationStateProvider
{
private readonly SecurityService securityService;
private ApplicationAuthenticationState authenticationState;
public ApplicationAuthenticationStateProvider(SecurityService securityService)
{
this.securityService = securityService;
}
public override async Task GetAuthenticationStateAsync()
{
var identity = new ClaimsIdentity();
try
{
var state = await GetApplicationAuthenticationStateAsync();
if (state.IsAuthenticated)
{
identity = new ClaimsIdentity(state.Claims.Select(c => new Claim(c.Type, c.Value)), "myapp.Server");
}
}
catch (HttpRequestException ex)
{
}
var result = new AuthenticationState(new ClaimsPrincipal(identity));
await securityService.InitializeAsync(result);
return result;
}
private async Task GetApplicationAuthenticationStateAsync()
{
if (authenticationState == null)
{
authenticationState = await securityService.GetAuthenticationStateAsync();
}
return authenticationState;
}
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... ch-refresh
Мобильная версия