public async Task Logout()
        {
            await _localStorage.RemoveAsync();

            ((ApiAuthenticationStateProvider)_authenticationStateProvider).MarkUserAsLoggedOut();
            _httpClient.DefaultRequestHeaders.Authorization = null;
        }
Ejemplo n.º 2
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
                });
            }
        }