public async Task <LoginResultDTO> Login(CredentialsDTO credentials)
        {
            var loginAsJson = JsonSerializer.Serialize(credentials);
            var response    = await _httpClient.PostAsync("api/Login", new StringContent(loginAsJson, Encoding.UTF8, "application/json"));

            var loginResult = JsonSerializer.Deserialize <LoginResultDTO>(await response.Content.ReadAsStringAsync(), new JsonSerializerOptions {
                PropertyNameCaseInsensitive = true
            });

            if (!response.IsSuccessStatusCode || !loginResult.Successful)
            {
                return(loginResult);
            }

            await _localStorage.StoreAsync(new GlobeLocalStorageData
            {
                Token    = loginResult.Token,
                UserName = credentials.UserName
            });

            await((ApiAuthenticationStateProvider)_authenticationStateProvider).MarkUserAsAuthenticated(credentials.UserName);
            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", loginResult.Token);

            return(loginResult);
        }
Beispiel #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
                });
            }
        }