예제 #1
0
        public async Task <ApiResponse <LoginUserVM> > LoginAsync(LoginUserVM userToLogin)
        {
            try
            {
                var loginResponse = await _httpClient.PostJTokenAsync <ApiResponse <LoginUserVM> >("api/account/login", userToLogin);

                if (loginResponse.IsError)
                {
                    return(loginResponse);
                }

                var loggedUser = loginResponse.Result;
                if (loggedUser.RememberMe)
                {
                    await _jsRuntime.InvokeVoidAsync("Cookies.set", "Ticket", loggedUser.Ticket, new { expires = 365 * 24 * 60 * 60 });

                    await _localStorage.SetItemAsync("Ticket", loggedUser.Ticket);
                }
                else
                {
                    await _jsRuntime.InvokeVoidAsync("Cookies.set", "Ticket", loggedUser.Ticket);

                    await _localStorage.RemoveItemAsync("Ticket");
                }

                return(loginResponse);
            }
            catch (Exception ex)
            {
                return(new ApiResponse <LoginUserVM>(StatusCodeType.Status500InternalServerError, "API threw an exception during the Login Attempt", null, null, ex));
            }
        }
        public async override Task <AuthenticationState> GetAuthenticationStateAsync()
        {
            try
            {
                var savedToken = await _localStorage.GetItemAsync <string>("authToken");

                if (string.IsNullOrWhiteSpace(savedToken))
                {
                    return(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));
                }
                var tokenContent = _tokenHandler.ReadJwtToken(savedToken);
                var expiry       = tokenContent.ValidTo;
                if (expiry < DateTime.Now)
                {
                    await _localStorage.RemoveItemAsync("authToken");

                    return(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));
                }

                //Get Claims from token and Build auth user object
                var claims = ParseClaims(tokenContent);
                var user   = new ClaimsPrincipal(new ClaimsIdentity(claims, "jwt"));
                var state  = new AuthenticationState(user);
                NotifyAuthenticationStateChanged(Task.FromResult(state));
                return(state);
            }
            catch (Exception)
            {
                return(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));
            }
        }
        public async Task <List <CatalogItem> > ListPaged(int pageSize)
        {
            string key        = "items";
            var    cacheEntry = await _localStorageService.GetItemAsync <CacheEntry <List <CatalogItem> > >(key);

            if (cacheEntry != null)
            {
                _logger.LogInformation("Loading items from local storage.");
                if (cacheEntry.DateCreated.AddMinutes(1) > DateTime.UtcNow)
                {
                    return(cacheEntry.Value);
                }
                else
                {
                    _logger.LogInformation($"Loading {key} from local storage.");
                    await _localStorageService.RemoveItemAsync(key);
                }
            }

            var items = await _catalogItemService.ListPaged(pageSize);

            var entry = new CacheEntry <List <CatalogItem> >(items);
            await _localStorageService.SetItemAsync(key, entry);

            return(items);
        }
        public override async Task <AuthenticationState> GetAuthenticationStateAsync()
        {
            ClaimsPrincipal user;

            if (await _localStorageService.ContainKeyAsync(Constants.ACCESS_TOKEN_KEY))
            {
                var token = await _localStorageService.GetItemAsync <string>(Constants.ACCESS_TOKEN_KEY);

                var securityToken = new JwtSecurityToken(token);
                if (securityToken.ValidTo < DateTime.UtcNow)
                {
                    await _localStorageService.RemoveItemAsync(Constants.ACCESS_TOKEN_KEY);
                }
                else
                {
                    user = _jwtService.GetIdentity(token);
                    return(await Task.FromResult(new AuthenticationState(user)));
                }
            }

            var identity = new ClaimsIdentity();

            user = new ClaimsPrincipal(identity);

            return(await Task.FromResult(new AuthenticationState(user)));
        }
예제 #5
0
 public async Task Logout()
 {
     await _localStorage.RemoveItemAsync("authToken");
     await _localStorage.RemoveItemAsync("currentTable");
     ((ApiAuthenticationStateProvider)_authenticationStateProvider).MarkUserAsLoggedOut();
     _httpClient.DefaultRequestHeaders.Authorization = null;
 }
        public override async Task <AuthenticationState> GetAuthenticationStateAsync()
        {
            var expiry = await _localStorage.GetItemAsync <DateTime>("authTokenExpiry");

            if (expiry != null && (expiry < DateTime.Now))
            {
                await _localStorage.RemoveItemAsync("authToken");

                await _localStorage.RemoveItemAsync("authTokenExpiry");

                var _x = new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
                NotifyAuthenticationStateChanged(Task.FromResult(_x));
                return(_x);
            }

            var savedToken = await _localStorage.GetItemAsync <string>("authToken");

            if (string.IsNullOrWhiteSpace(savedToken))
            {
                return(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));
            }

            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", savedToken);

            var id = await _localStorage.GetItemAsync <string>("userId");

            _state.User = await _httpClient.GetJsonAsync <UserModel>($"api/users/{id}");

            var x = new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(ParseClaimsFromJwt(savedToken), "jwt")));

            return(x);
        }
        public override async Task <AuthenticationState> GetAuthenticationStateAsync()
        {
            var accessToken = await localStorage.GetItemAsync <string>(nameof(SignInResultDto.TokenPair.AccessToken));

            if (string.IsNullOrWhiteSpace(accessToken))
            {
                await DeleteTokenFromConnectionClients();

                return(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));
            }

            try
            {
                var claims = accessToken.ParseClaimsFromJwt();
                await ProvideTokenToConnectionClients(accessToken);

                return(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(claims, "jwt"))));
            }
            catch (System.Exception)
            {
                await DeleteTokenFromConnectionClients();

                await localStorage.RemoveItemAsync(nameof(SignInResultDto.TokenPair.AccessToken));

                await localStorage.RemoveItemAsync(nameof(SignInResultDto.TokenPair.RefreshToken));

                return(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));
            }
        }
        public override async Task <AuthenticationState> GetAuthenticationStateAsync()
        {
            var token = await localStorage.GetItemAsync <Token>(token_key);

            if (token == null || string.IsNullOrWhiteSpace(token.AuthToken))
            {
                flurlClient.HttpClient.DefaultRequestHeaders.Authorization = null;
                return(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));
            }

            var dateNow = DateTime.UtcNow;

            if (dateNow > token.ExpireDateUtc)
            {
                await localStorage.RemoveItemAsync(token_key);

                return(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));
            }

            // This is valid because we know the token lasts 7 days (604800 seconds)
            else if (dateNow.AddDays(2) >= token.ExpireDateUtc)
            {
                _ = RenewTokenAsync(token);
            }

            flurlClient.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token.AuthToken);

            return(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(ParseClaimsFromJwt2(token.IdToken), "jwt"))));
        }
        private async Task CleanJwtStorage()
        {
            await localStorageService.RemoveItemAsync(localStorageOption.Token);

            await localStorageService.RemoveItemAsync(localStorageOption.ExpirationDate);

            httpClient.DefaultRequestHeaders.Authorization = null;
        }
예제 #10
0
        public async Task Logout()
        {
            await _localStorage.RemoveItemAsync("authToken");

            await _localStorage.RemoveItemAsync("refreshToken");

            _httpClient.DefaultRequestHeaders.Authorization = null;
        }
        public async Task ClearAuthenticationAsync()
        {
            await _localStorageService.RemoveItemAsync("userInfo");

            await _localStorageService.RemoveItemAsync("userRoles");

            await Task.CompletedTask;
        }
        public async Task Logout()
        {
            await _localStorage.RemoveItemAsync("authToken");

            await _localStorage.RemoveItemAsync("LoginName");

            ((ApiAuthenticationStateProvider)_authenticationStateProvider).LoggedOut();
        }
예제 #13
0
        private async Task RemoveTokens()
        {
            await _localStorage.RemoveItemAsync(AuthDescriptions.TokenKey);

            await _localStorage.RemoveItemAsync(AuthDescriptions.RefreshKey);

            _httpClient.DefaultRequestHeaders.Authorization = null;
        }
예제 #14
0
        private async Task DeleteLocalStorage()
        {
            await _localStorage.RemoveItemAsync("authToken");

            await _localStorage.RemoveItemAsync("username");

            await _localStorage.RemoveItemAsync("inDocker");
        }
예제 #15
0
        public async Task LogUserOut()
        {
            await _localStorage.RemoveItemAsync("authToken");

            await _localStorage.RemoveItemAsync("profileImage");

            ((ConduitAuthenticationStateProvider)_authenticationStateProvider).MarkUserAsLoggedOut();
        }
        public async Task Logout()
        {
            await _localStorage.RemoveItemAsync(SD.Local_Token);

            await _localStorage.RemoveItemAsync(SD.Local_UserDetails);

            _client.DefaultRequestHeaders.Authorization = null;
        }
예제 #17
0
        /// <summary>
        /// Clears all items from LocalStorage.
        /// </summary>
        public async Task Clear()
        {
            await localStorage.RemoveItemAsync(BasketCookieName);

            await localStorage.RemoveItemAsync(RecipesCookieName);

            addedRecipes = new List <Recipe>();
            basketItems  = new List <BasketItem>();
        }
예제 #18
0
        public async Task Logout()
        {
            await _localStorage.RemoveItemAsync("at");

            await _localStorage.RemoveItemAsync("rt");

            ((CustomAuthStateProvider)_authenticationStateProvider).LoggedOut();
            _httpClient.DefaultRequestHeaders.Authorization = null;
        }
        public async Task LogoutAsync()
        {
            await _localStorageService.RemoveItemAsync("9gag-auth-token");

            await _localStorageService.RemoveItemAsync("9gag-auth-token-expiration-date");

            _apiClient.AuthenticationInfo.ClearToken();
            NotifyAuthenticationStateChanged();
        }
예제 #20
0
        public async Task Logout()
        {
            await _localStorage.RemoveItemAsync("authToken");

            await _localStorage.RemoveItemAsync("username");

            UserName   = null;
            IsLoggedIn = false;
        }
예제 #21
0
        /// <inheritdoc />
        public async void Logout()
        {
            await _localStorageService.RemoveItemAsync("user");

            await _localStorageService.RemoveItemAsync("token");

            _storeService.Remove("user");
            _navigationManager.NavigateTo("login");
        }
예제 #22
0
        public async Task Signout()
        {
            await localStorage.RemoveItemAsync("authToken");

            await localStorage.RemoveItemAsync("user");

            ((ApiAuthenticationStateProvider)authStateProvider).MarkUserAsLoggedOut();
            httpClient.DefaultRequestHeaders.Authorization = null;
        }
예제 #23
0
        public async Task SingOutAsync()
        {
            await _localStorage.RemoveItemAsync("authToken");

            await _localStorage.RemoveItemAsync("authRefreshToken");

            ((BaseApiAuthenticationStateProvider)_authenticationStateProvider).MarkUserAsLoggedOut();
            //_httpClient.DefaultRequestHeaders.Authorization = null;
        }
예제 #24
0
        public async Task Logout()
        {
            await _localStorage.RemoveItemAsync(SD.Local_Token);

            await _localStorage.RemoveItemAsync(SD.Local_UserDetails);

            ((AuthStateProvider)_authenticationStateProvider).NotifyUserLogout();
            _httpClient.DefaultRequestHeaders.Authorization = null;
        }
예제 #25
0
        public async Task Logout()
        {
            await _localStorage.RemoveItemAsync("authToken");

            await _localStorage.RemoveItemAsync("refreshToken");

            ((AuthStateProvider)_authStateProvider).NotifyUserLogout();
            _client.DefaultRequestHeaders.Authorization = null;
        }
        public async Task <IResult> Logout()
        {
            await _localStorage.RemoveItemAsync("authToken");

            await _localStorage.RemoveItemAsync("refreshToken");

            ((BlazorHeroStateProvider)_authenticationStateProvider).MarkUserAsLoggedOut();
            _httpClient.DefaultRequestHeaders.Authorization = null;
            return(Result.Success());
        }
        public void MarkUserAsLoggedOut()
        {
            _localStorageService.RemoveItemAsync("accessToken");
            _localStorageService.RemoveItemAsync("refreshToken");

            var identity = new ClaimsIdentity();
            var user     = new ClaimsPrincipal(identity);

            NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(user)));
        }
예제 #28
0
        public async Task ClearLocalStorage()
        {
            await _localStorage.RemoveItemAsync("authToken");

            await _localStorage.RemoveItemAsync("fullName");

            await _localStorage.RemoveItemAsync("userEmail");

            await _localStorage.RemoveItemAsync("userCultureId");
        }
        public async Task Logout()
        {
            await _localStorage.RemoveItemAsync("authToken");

            await _localStorage.RemoveItemAsync("LoginName");

            var client = _client.CreateClient();

            client.DefaultRequestHeaders.Authorization = null;
            ((ApiAuthenticationStateProvider)_authenticationStateProvider).LoggedOut();
        }
예제 #30
0
        public async Task MarkUserAsLoggedOut()
        {
            var anonymousUser = new ClaimsPrincipal(new ClaimsIdentity());
            var authState     = Task.FromResult(new AuthenticationState(anonymousUser));
            await _localStorage.RemoveItemAsync("authToken");

            await _localStorage.RemoveItemAsync("tokenExpire");

            _genericRepository.SetToken(string.Empty);
            NotifyAuthenticationStateChanged(authState);
        }