using System.Net.Http; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; namespace BlazingPizza.Client { public class ServerAuthenticationStateProvider : AuthenticationStateProvider { private readonly HttpClient _httpClient; public ServerAuthenticationStateProvider(HttpClient httpClient) { _httpClient = httpClient; } public override async Task GetAuthenticationStateAsync() { var userInfo = await _httpClient.GetJsonAsync("user"); var identity = userInfo.IsAuthenticated ? new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, userInfo.Name) }, "serverauth") : new ClaimsIdentity(); return new AuthenticationState(new ClaimsPrincipal(identity)); } } }