Пример #1
0
        async public Task MarkUserAsAuthenticated(string userName)
        {
            var data = await _localStorage.GetAsync();

            var authenticatedUser = ClaimsPrincipalGenerator.BuildClaimsPrincipal(data.Token, userName);
            var authState         = Task.FromResult(new AuthenticationState(authenticatedUser));

            NotifyAuthenticationStateChanged(authState);
        }
Пример #2
0
        public override async Task <AuthenticationState> GetAuthenticationStateAsync()
        {
            var data = await _localStorage.GetAsync();

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

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

            return(new AuthenticationState(ClaimsPrincipalGenerator.BuildClaimsPrincipal(data.Token, data.UserName)));
        }
Пример #3
0
        async public Task <LoginResult> LoginAsync(Credentials credentials)
        {
            if (string.IsNullOrWhiteSpace(credentials.UserName) || string.IsNullOrWhiteSpace(credentials.Password) || !_credentials.ContainsKey(credentials.UserName))
            {
                await _globeDataStorage.RemoveAsync();

                OnPrincipalChanged(new AnonymousPrincipal());

                return(await Task.FromResult(new LoginResult
                {
                    Successful = false,
                    Error = "Invalid Credentials",
                    Token = string.Empty
                }));
            }

            var result = _credentials[credentials.UserName] == credentials.Password;

            if (!result)
            {
                await _globeDataStorage.RemoveAsync();

                OnPrincipalChanged(new AnonymousPrincipal());

                return(await Task.FromResult(new LoginResult
                {
                    Successful = false,
                    Error = "Invalid Credentials",
                    Token = string.Empty
                }));
            }

            await _globeDataStorage.StoreAsync(new GlobeLocalStorageData
            {
                Token    = "Valid Token",
                UserName = credentials.UserName
            });

            OnPrincipalChanged(ClaimsPrincipalGenerator.BuildClaimsPrincipal("Valid Token", credentials.UserName));

            return(await Task.FromResult(new LoginResult
            {
                Successful = true,
                Error = string.Empty,
                Token = "Valid Token"
            }));
        }
        async public Task <LoginResult> LoginAsync(Credentials credentials)
        {
            try
            {
                var loginResult = await OnLoginAsync(credentials);

                if (loginResult.Successful)
                {
                    await _globeDataStorage.StoreAsync(new GlobeLocalStorageData
                    {
                        Token    = loginResult.Token,
                        UserName = credentials.UserName
                    });
                    await OnPrincipalChanged(ClaimsPrincipalGenerator.BuildClaimsPrincipal(loginResult.Token, credentials.UserName));
                }
                else
                {
                    await _globeDataStorage.RemoveAsync();
                    await OnPrincipalChanged(new AnonymousPrincipal());
                }

                return(loginResult);
            }
            catch (Exception e)
            {
                _logger.Error(e, "Login Failed");

                await _globeDataStorage.RemoveAsync();
                await OnPrincipalChanged(new AnonymousPrincipal());

                return(new LoginResult
                {
                    Successful = false,
                    Error = e.Message,
                    Token = string.Empty
                });
            }
        }