protected override async Task OnInitializedAsync() { base.OnInitialized(); await Task.Delay(2000); var loggedinUser = (await AuthenticationStateTask).User; if (loggedinUser.IsInRole("Admin")) { Console.WriteLine($"User {loggedinUser.Identity.Name} is in role admin"); } //var user = await LocalStorage.GetItemAsync<UserDto>(MagicStrings.Local_UserDetails); var token = await LocalStorage.GetItemAsync <string>(MagicStrings.Local_Token); var id = JwtParser.ParseIdFromJwt(token); var user = await AuthenticationApiService.GetUserAsync(id); AuthenticatedUser = new UserDetailsDto(); MiniMapper.CopyProperties(AuthenticatedUser, user); AuthenticatedUser.Role = JwtParser.ParseRolesFromJwt(token).FirstOrDefault(); FormUser = new UserDetailsDto(); MiniMapper.CopyProperties(FormUser, AuthenticatedUser); EditContext = new EditContext(FormUser); EditContext.OnFieldChanged += EditContext_OnFieldChanged; }
public async Task <ApiResponseDto> Logout() { //UserDto userDto = await _localStorage.GetItemAsync<UserDto>(MagicStrings.Local_UserDetails); var token = await _localStorage.GetItemAsync <string>(MagicStrings.Local_Token); if (token == null) { return(new ApiResponseDto { IsSuccessful = false, Errors = new string[] { "logout failed, no token" } }); } string id = JwtParser.ParseIdFromJwt(token); if (id == null) { return(new ApiResponseDto { IsSuccessful = false, Errors = new string[] { "logout failed, no id in token" } }); } var response = await _client.GetAsync($"api/account/logout/{id}"); if (!response.IsSuccessStatusCode) { return(new ApiResponseDto { IsSuccessful = false, Errors = new string[] { response.StatusCode.ToString() } }); } await _localStorage.RemoveItemAsync(MagicStrings.Local_Token); //await _localStorage.RemoveItemAsync(MagicStrings.Local_UserDetails); _client.DefaultRequestHeaders.Authorization = null; ((MyAuthenticationStateProvider)_authStateProvider).NotifyUserLogout(); return(new ApiResponseDto { IsSuccessful = true }); }
public void JwtParser_GetId_ShouldReturnCorrectId() { var mail = JwtParser.ParseIdFromJwt(token); Assert.AreEqual("0534dfbb-2df2-4805-8c88-1fa199c85ab4", mail); }