public async override Task <AuthenticationState> GetAuthenticationStateAsync() { if (await _localStorage.ContainKeyAsync("id") && await _localStorage.ContainKeyAsync("role")) { var id = await _localStorage.GetItemAsync <string>("id"); var role = await _localStorage.GetItemAsync <string>("role"); var userClaims = new List <Claim>() { new Claim(ClaimTypes.Name, id), new Claim(ClaimTypes.Role, role) }; var claimsId = new ClaimsIdentity(userClaims, "User Identity"); var userPrincipal = new ClaimsPrincipal(new[] { claimsId }); return(new AuthenticationState(userPrincipal)); } else { return(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()))); } }
public override async Task <AuthenticationState> GetAuthenticationStateAsync() { try { if (!await _LocalStorage.ContainKeyAsync(Data.ConventionalKeys.TokenStorageKey)) { return(new AuthenticationState(new ClaimsPrincipal())); } string tokenString = await _LocalStorage.GetItemAsync <string>(Data.ConventionalKeys.TokenStorageKey); if (!_TokenHandler.CanReadToken(tokenString)) { return(await AnonymousState()); } var token = _TokenHandler.ReadJwtToken(tokenString); if (token.ValidTo.CompareTo(DateTime.Now) < 0) { return(await AnonymousState()); } var claims = GetClaims(token); var user = new ClaimsPrincipal(new ClaimsIdentity(claims, "jwt")); return(new AuthenticationState(user)); } catch (Exception) { return(new AuthenticationState(new ClaimsPrincipal())); } }
protected async Task <bool> AddBearerToken(bool ignoreExpired = false) { if (await _localStorage.ContainKeyAsync("expiry")) { var expiration = await _localStorage.GetItemAsync <string>("expiry"); var expiry = DateTime.Parse(expiration); if (DateTime.Now > expiry) { await _authenticationService?.Logout(); if (ignoreExpired) { return(true); } _navigation.NavigateTo("login"); return(false); } } if (await _localStorage.ContainKeyAsync("token")) { _client.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await _localStorage.GetItemAsync <string>("token")); } return(true); }
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))); }
protected override async Task <GlobalGameState?> InternalLoadAsync() { if (!await _localStorage.ContainKeyAsync(SaveGameLocalStorageKey)) { return(null); } return(await _localStorage.GetItemAsync <GlobalGameState>(SaveGameLocalStorageKey)); }
public async Task <bool> RemoveTokensAsync() { await _localStorageService.RemoveItemAsync(_options.Value.AccessTokenName); await _localStorageService.RemoveItemAsync(_options.Value.RefreshTokenName); if (!(await _localStorageService.ContainKeyAsync(_options.Value.AccessTokenName) && await _localStorageService.ContainKeyAsync(_options.Value.RefreshTokenName))) { return(false); } return(true); }
public async Task RemoveGroceryList(string Id) { if (await _localStorageService.ContainKeyAsync(_key)) { var data = await _localStorageService.GetItemAsync <List <GroceryList> >(_key); if (data != null) { var toRemove = data.FirstOrDefault(f => f.Key == Id); data.Remove(toRemove); await UpsertAllGroceryLists(data); } } }
public async Task InitAsync() { if (await _storage.ContainKeyAsync(StorageKey)) { return; } var state = await _authenticationStateProvider.GetAuthenticationStateAsync(); if (state.User.Identity?.IsAuthenticated == true) { _navigation.NavigateTo(Links.Authentication.LogIn(Uri.EscapeDataString(_navigation.Uri))); } }
public async override Task <AuthenticationState> GetAuthenticationStateAsync() { try { if (await _storageService.ContainKeyAsync("access_token")) { //Decrypt the token to use the content string accessToken = await _storageService.GetItemAsStringAsync("access_token"); var handler = new JwtSecurityTokenHandler(); var jwt = handler.ReadJwtToken(accessToken); var identity = new ClaimsIdentity(jwt.Claims, "Bearer"); var user = new ClaimsPrincipal(identity); var state = new AuthenticationState(user); //Notify the application of the state change NotifyAuthenticationStateChanged(Task.FromResult(state)); //return the user with all the claims. return(state); } else { //return an empty claims principal return(new AuthenticationState(new System.Security.Claims.ClaimsPrincipal())); } } catch (Exception e) { Console.WriteLine(e.Message); //Return an empty claims principal return(new AuthenticationState(new System.Security.Claims.ClaimsPrincipal())); } }
public async override Task <AuthenticationState> GetAuthenticationStateAsync() { if (await _storageService.ContainKeyAsync("User")) { var userInfo = await _storageService.GetItemAsync <User>("User"); var handler = new JwtSecurityTokenHandler(); var tokenS = handler.ReadJwtToken(userInfo.Token); var claimItems = tokenS.Claims.ToList(); List <Claim> claims = new List <Claim>(); foreach (var item in claimItems) { claims.Add(new Claim(item.Type, item.Value)); } //var claims = new[] //{ // new Claim("Email", claimItems["]), // new Claim("FirstName", "achal"), // new Claim("LastName", "parashar"), // new Claim("AccessToken", userInfo.Token), // new Claim(ClaimTypes.NameIdentifier, userInfo.UserId.ToString()), //}; var identity = new ClaimsIdentity(claims, "BearerToken"); var user = new ClaimsPrincipal(identity); var state = new AuthenticationState(user); NotifyAuthenticationStateChanged(Task.FromResult(state)); return(state); } return(new AuthenticationState(new ClaimsPrincipal())); }
public async override Task <AuthenticationState> GetAuthenticationStateAsync() { if (await _storageService.ContainKeyAsync("User")) { var userInfo = await _storageService.GetItemAsync <UserResponse>("User"); if (userInfo.ExpiredDate < DateTime.Now || (userInfo.RememberMe == false && !FirstLogin)) { await LogoutAsync(); return(new AuthenticationState(new ClaimsPrincipal())); } var claims = new[] { new Claim(ClaimTypes.Name, userInfo.Name), new Claim(ClaimTypes.NameIdentifier, userInfo.UserName), new Claim("AccessToken", userInfo.Token), new Claim("Expired", userInfo.ExpiredDate.ToString()), new Claim("RememberMe", userInfo.RememberMe.ToString()), new Claim(ClaimTypes.Role, userInfo.Role) }; var identity = new ClaimsIdentity(claims, "Bearer"); var user = new ClaimsPrincipal(identity); CurrentUser = new AuthenticationState(user); NotifyAuthenticationStateChanged(Task.FromResult(CurrentUser)); return(CurrentUser); } return(new AuthenticationState(new ClaimsPrincipal())); }
public async Task InitializeAsync() { int highScore = await _localStorage.GetItemAsync <int>(Constants.LS_HIGH_SCORE); if (highScore > 0) { HighScore = highScore; } if (await _localStorage.ContainKeyAsync(Constants.LS_TILES)) { Tiles = await _localStorage.GetItemAsync <Tile[]>(Constants.LS_TILES); foreach (var tile in Tiles) { tile.NewTile = true; } await CalcScoreAsync(); } else { await ResetBoardAsync(); } IsInitialized = true; }
public async override Task <AuthenticationState> GetAuthenticationStateAsync() { if (await _storageService.ContainKeyAsync("user")) { var userInfo = await _storageService.GetItemAsync <LocalUserInfo>("user"); var claims = new[] { new Claim("Email", userInfo.Email), new Claim("FirstName", userInfo.FirstName), new Claim("LastName", userInfo.LastName), new Claim("AccessToken", userInfo.AccessToken), new Claim(ClaimTypes.NameIdentifier, userInfo.Id), }; var identity = new ClaimsIdentity(claims, "BearerToken"); var user = new ClaimsPrincipal(identity); var state = new AuthenticationState(user); NotifyAuthenticationStateChanged(Task.FromResult(state)); return(state); } return(new AuthenticationState(new ClaimsPrincipal())); }
public async override Task <AuthenticationState> GetAuthenticationStateAsync() { try { if (await _storageService.ContainKeyAsync("access_token")) { string accessToken = await _storageService.GetItemAsStringAsync("access_token"); var handler = new JwtSecurityTokenHandler(); var jwt = handler.ReadJwtToken(accessToken); var identity = new ClaimsIdentity(jwt.Claims, "Bearer"); var user = new ClaimsPrincipal(identity); var state = new AuthenticationState(user); NotifyAuthenticationStateChanged(Task.FromResult(state)); return(state); } else { return(new AuthenticationState(new ClaimsPrincipal())); } } catch (Exception e) { Console.WriteLine(e.Message); return(new AuthenticationState(new ClaimsPrincipal())); } }
private async Task ClearPriorData() { if (await _localStorage.ContainKeyAsync(key)) { await _localStorage.RemoveItemAsync(key); } }
public async override Task <AuthenticationState> GetAuthenticationStateAsync() { //Any object in local storage as named as User. if (await _storageService.ContainKeyAsync("User")) { //Create User var userInfo = await _storageService.GetItemAsync <LocalUserInfo>("User"); var claims = new[] { new Claim("Email", userInfo.Email), new Claim("FirstName", userInfo.FirstName), new Claim("LastName", userInfo.LastName), new Claim("AccessToken", userInfo.AccessToken), new Claim(ClaimTypes.NameIdentifier, userInfo.Id) }; var identity = new ClaimsIdentity(claims, "BearerToken"); var user = new ClaimsPrincipal(identity); var state = new AuthenticationState(user); //Blazor app doesn't automatically notify the change. NotifyAuthenticationStateChanged(Task.FromResult(state)); return(state); } return(new AuthenticationState(new ClaimsPrincipal())); }
protected async Task AddBearerToken() { if (await _localStorage.ContainKeyAsync("token")) { _client.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await _localStorage.GetItemAsync <string>("token")); } }
public async Task <bool> IsLoggedIn() { var existsInLocal = await _localStorage.ContainKeyAsync(AccountKey); var existsInSession = (await _sessionStorage.GetItemAsync <AuthorizedUser>(AccountKey)) != null; // Change when ContainKeyAsync is added return(existsInLocal || existsInSession); }
private async Task <Guid> GetActiveProfileIdAsync() { if (!await _localStorageService.ContainKeyAsync(ACTIVE_PROFILE_KEY)) { return(Guid.Empty); } return(await _localStorageService.GetItemAsync <Guid>(ACTIVE_PROFILE_KEY)); }
public async Task Logout() { if (await _localStorage.ContainKeyAsync("token")) { await _localStorage.RemoveItemAsync("token"); } ((AuthStateProvider)_authStateProvider).NotifyUserLogout(); }
public async Task Resume() { if (await _storage.ContainKeyAsync("auth")) { User = await _storage.GetItemAsync <AuthUser>("auth"); await ApiUtils.RefreshUser(); Task.Run(async() => { while (true) { await Task.Delay(100); await _storage.SetItemAsync("auth", User); } }); } }
internal async Task <DBUser> GetUser() { try { if (string.IsNullOrWhiteSpace(_currentToken)) { if (await _localData.ContainKeyAsync(tokenKey)) { _currentToken = await _localData.GetItemAsync <string>(tokenKey); } } } catch { } if (!string.IsNullOrWhiteSpace(_currentToken)) { _httpWorker.LoggedIn(_currentToken); if (CurrentUser != null) { return(CurrentUser); } try { var user = await _sessionData.GetItemAsync <DBUser>(tokenUser); if (user != null) { CurrentUser = user; return(user); } } catch { } var res = await _memberHandler.TouchLogin(); if (res == null) { await Logout(); return(null); } else { CurrentUser = res; await _sessionData.SetItemAsync(tokenUser, CurrentUser); return(res); } } Console.WriteLine("Asking For Info: " + _currentToken); return(null); }
public static async Task RemoveLocalStorageItem(this ILocalStorageService localStorage, string key) { var contains = await localStorage.ContainKeyAsync(key); if (contains) { await localStorage.RemoveItemAsync(key); } }
public async override Task <AuthenticationState> GetAuthenticationStateAsync() { if (await _localStorage.ContainKeyAsync(USER_KEY)) { var userToken = await _localStorage.GetItemAsync <IdentityApiResponse>(USER_KEY); // Get the user claims var claims = ExtractUserClaims(userToken.AccessToken); // TODO: Refresh the token if it's expired var claimsIdentity = new ClaimsIdentity(claims, "JwtBearerToken"); var user = new ClaimsPrincipal(claimsIdentity); return(new AuthenticationState(user)); } // Empty user return(new AuthenticationState(new ClaimsPrincipal())); }
public async override Task <AuthenticationState> GetAuthenticationStateAsync() { try { if (_storageService != null && await _storageService.ContainKeyAsync("User")) { var userInfo = await _storageService.GetItemAsync <LocalUserInfo>("User"); var claims = new[] { new Claim("Email", userInfo.Email), new Claim("FirstName", userInfo.FirstName), new Claim("LastName", userInfo.LastName), new Claim("AccessToken", userInfo.AccessToken), new Claim(ClaimTypes.NameIdentifier, userInfo.Id), new Claim("IsAdmin", (userInfo.IsAdmin)?"true":"false"), }; var identity = new ClaimsIdentity(claims, "BearerToken"); var user = new ClaimsPrincipal(identity); var state = new AuthenticationState(user); //Services.AppState appState = new Services.AppState(); //appState.IsAdmin = userInfo.IsAdmin; Services.AppState.PutData(userInfo.Email, true); NotifyAuthenticationStateChanged(Task.FromResult(state)); return(state); } } catch (Exception e) { //Console.WriteLine(e.ToString()); } finally { //Services.AppState.PutData("*****@*****.**", true); } return(new AuthenticationState(new ClaimsPrincipal())); //////// var identity1 = new ClaimsIdentity(new[] ////////{ //////// new Claim(ClaimTypes.Name, "mrfibuli"), //////// }, "Fake authentication type"); //////// return new AuthenticationState(new ClaimsPrincipal(identity1)); // var identity = new ClaimsIdentity(new[] //{ // new Claim(ClaimTypes.Name, "mrfibuli"), // }, "Fake authentication type"); // var user = new ClaimsPrincipal(identity); // var state = new AuthenticationState(user); // NotifyAuthenticationStateChanged(Task.FromResult(state)); // return state; }
public override async Task <AuthenticationState> GetAuthenticationStateAsync() { var loggedin = await _loclStorageService.ContainKeyAsync("jwt"); if (!loggedin) { return(new AuthenticationState(new ClaimsPrincipal(_unauthorized))); } return(await BuildAuthenticationState()); }
protected async override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { if (await _storageService.ContainKeyAsync("access_token")) { string accessToken = await _storageService.GetItemAsStringAsync("access_token"); request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken); } return(await base.SendAsync(request, cancellationToken)); }
public override async Task <AuthenticationState> GetAuthenticationStateAsync() { bool isAccessTokenExist = await _localStorageService.ContainKeyAsync(_options.Value.AccessTokenName); bool isRefreshTokenExist = await _localStorageService.ContainKeyAsync(_options.Value.RefreshTokenName); if (!(isAccessTokenExist || isRefreshTokenExist)) { var anonymousUser = new ClaimsPrincipal(new ClaimsIdentity()); return(new AuthenticationState(anonymousUser)); } var token = await _tokenStorageService.GetTokenAsync(); var claims = _tokenProvider.ParseToken(token); var user = new ClaimsPrincipal(claims); return(new AuthenticationState(user)); }
public static async Task OpenWindowAsync(IJSRuntime jsRuntime, ILocalStorageService localStorage, string url, string feature = "toolbar=no,scrollbars=yes,resizable=yes,top=500,left=500,width=450,height=500") { if (await localStorage.ContainKeyAsync("token")) { await localStorage.RemoveItemAsync("token"); } if (await localStorage.ContainKeyAsync("fetching")) { await localStorage.RemoveItemAsync("fetching"); } await jsRuntime.InvokeVoidAsync("Arc4u.CreatePopupWindow", url, feature); do { await Task.Delay(100); }while (null == await localStorage.GetItemAsync <string>("fetching")); }
private async Task UpsertMealsList(List <Meals> allMealsData) { if (await _localStorageService.ContainKeyAsync(_key)) { await _localStorageService.RemoveItemAsync(_key); } allMealsData = allMealsData.OrderBy(a => a.DateTime).ToList(); await _localStorageService.SetItemAsync(_key, allMealsData); }