diff options
Diffstat (limited to 'wasm/BlazingPizza.Client/ServerAuthenticationStateProvider.cs')
-rw-r--r-- | wasm/BlazingPizza.Client/ServerAuthenticationStateProvider.cs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/wasm/BlazingPizza.Client/ServerAuthenticationStateProvider.cs b/wasm/BlazingPizza.Client/ServerAuthenticationStateProvider.cs new file mode 100644 index 0000000..9a94bc5 --- /dev/null +++ b/wasm/BlazingPizza.Client/ServerAuthenticationStateProvider.cs @@ -0,0 +1,28 @@ +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<AuthenticationState> GetAuthenticationStateAsync() + { + var userInfo = await _httpClient.GetJsonAsync<UserInfo>("user"); + + var identity = userInfo.IsAuthenticated + ? new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, userInfo.Name) }, "serverauth") + : new ClaimsIdentity(); + + return new AuthenticationState(new ClaimsPrincipal(identity)); + } + } +} |