Пример #1
0
        public override async Task <AuthenticationState> GetAuthenticationStateAsync()
        {
            if (_accessor.IsServerStarted())
            {
                _token = await _storage.GetAsync <string>(TokenKey);
            }

            Console.WriteLine($"Token is {_token}");

            if (!string.IsNullOrWhiteSpace(_token))
            {
                var jwtToken = new JwtSecurityToken(_token);

                if (jwtToken.ValidTo > DateTime.UtcNow)
                {
                    _httpClientService.SetAuthorization(_token);

                    var identity = new ClaimsIdentity(jwtToken.Claims, AuthenticationType);

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

            _token = null;

            if (_accessor.IsServerStarted())
            {
                await _storage.DeleteAsync(TokenKey);
            }

            _httpClientService.RemoveAuthorization();

            return(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));
        }
Пример #2
0
        public async Task <int?> GetSavedPlayerIdAsync()
        {
            var savedPlayerId = await _preferenceStore.GetAsync <int?>(PreferredPlayerIdKey);

            var players = await _playerService.GetPlayersAsync();

            var savedPlayerIsExistingPlayer = savedPlayerId.HasValue && players.ContainsKey(savedPlayerId.Value);

            return(savedPlayerIsExistingPlayer ? savedPlayerId : null);
        }
Пример #3
0
        private async Task <T> GetStoredItemOrResetAsync <T>(string key, T defaultValue = default)
        {
            try {
                return(await _localStorageService.GetAsync <T>(key));
            } catch {
                await _localStorageService.DeleteAsync(key);

                return(defaultValue);
            }
        }
Пример #4
0
        public async Task <bool> CheckIfAuthenticated()
        {
            var token = await _storage.GetAsync <string>("token");

            if (!string.IsNullOrEmpty(token.Value))
            {
                UserToken  = token.Value;
                IsLoggedIn = true;
                return(true);
            }
            return(false);
        }
Пример #5
0
        public async Task ValidFormSubmitted(EditContext editContext)
        {
            authorization_result        = null;
            email_validation_message    = null;
            password_validation_message = null;

            LoginModel model = (LoginModel)editContext.Model;

            if (model.email == "" || model.email == null)
            {
                email_validation_message = Localizer.GetText("Required");
            }
            if (model.password == "" || model.password == null)
            {
                password_validation_message = Localizer.GetText("Required");
            }

            if (email_validation_message != null || password_validation_message != null)
            {
                return;
            }


            var security_result = await _security.Authenticate(loginModel.email, loginModel.password);

            if (security_result.Item1)
            {
                await ProtectedSessionStore.SetAsync("GUID", security_result.Item2);

                var Locale = await ProtectedLocalStorage.GetAsync <string>("Culture");

                if (Locale != null && Locale != "Select Culture")
                {
                    LocOptions.Value.SetDefaultCulture(Locale);
                }

                navigationManager.NavigateTo("/main", forceLoad: true);
            }
            else
            {
                authorization_result = Localizer.GetText("WrongPassword");
                loginModel.email     = null;
                loginModel.password  = null;
            }
        }
Пример #6
0
        public async Task <HttpResponseMessage> CreateRoleAsync(string roleName)
        {
            string token = await protectedLocalStorage.GetAsync <string>(TokenKey);

            string serializedData = JsonConvert.SerializeObject(roleName);
            var    requestMessage = new HttpRequestMessage(HttpMethod.Post, "CreateRoleAsync")
            {
                Content = new StringContent(serializedData)
            };

            requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            requestMessage.Headers.Authorization       = new AuthenticationHeaderValue("bearer", token);
            var response = await httpClient.SendAsync(requestMessage);

            return(response);
        }
Пример #7
0
        public async Task <IEnumerable <ShowAllFriend> > GetAllFriendsAsync(string userId)
        {
            string token = await protectedLocalStorage.GetAsync <string>(TokenKey);

            string serializedData = JsonConvert.SerializeObject(userId);
            var    requestMessage = new HttpRequestMessage(HttpMethod.Get, "GetAllFriendsAsync");

            requestMessage.Content = new StringContent(serializedData);
            requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            requestMessage.Headers.Authorization       = new AuthenticationHeaderValue("bearer", token);
            var response = await httpClient.SendAsync(requestMessage);

            IEnumerable <ShowAllFriend> friendsList = new List <ShowAllFriend>();

            if (response.IsSuccessStatusCode)
            {
                friendsList = JsonConvert.DeserializeObject <IEnumerable <ShowAllFriend> >(await response.Content.ReadAsStringAsync());
                return(friendsList);
            }

            return(friendsList);
        }