public async Task SetUserAsAuthenticated(CurrentUserResource user) { await _localStorageService.SetItemAsync("accessToken", user.AccessToken); await _localStorageService.SetItemAsync("refreshToken", user.RefreshToken); var identity = GetClaimsIdentity(user); var claimsPrincipal = new ClaimsPrincipal(identity); NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(claimsPrincipal))); }
private ClaimsIdentity GetClaimsIdentity(CurrentUserResource user) { var claimsIdentity = new ClaimsIdentity(); if (user.NameIdentifier != null) { claimsIdentity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.NameIdentifier, user.NameIdentifier), new Claim(ClaimTypes.Name, user.Name), new Claim(ClaimTypes.Email, user.Email), new Claim(ClaimTypes.Role, user.Role), }, "apiauth_type"); } return(claimsIdentity); }
public override async Task <AuthenticationState> GetAuthenticationStateAsync() { var accessToken = await _localStorageService.GetItemAsync <string>("accessToken"); ClaimsIdentity identity; if (accessToken != null && accessToken != string.Empty) { CurrentUserResource user = await _authService.GetUserByAccessTokenAsync(accessToken); identity = GetClaimsIdentity(user); } else { identity = new ClaimsIdentity(); } var claimsPrincipal = new ClaimsPrincipal(identity); return(await Task.FromResult(new AuthenticationState(claimsPrincipal))); }