Exemplo n.º 1
0
        public async Task StartLoginProcedureAsync()
        {
            if (TokenRetrieved == null)
            {
                throw new FieldAccessException($"No handler set for {nameof(TokenRetrieved)} event.");
            }

            if (Closed == null)
            {
                throw new FieldAccessException($"No handler set for {nameof(Closed)} event.");
            }

            var session = await CreateLoginSessionAsync();

            //open login window
            Uri webAppBaseUri = new Uri(_webAppBaseUrl);
            Uri loginPageUri  = new Uri(webAppBaseUri, $"login?s={session.PublicIdentifier}");

            OpenUrlInBrowser(loginPageUri.AbsoluteUri);

            var maxLoginTimeInSeconds         = 90;
            var pollingIntervalInMilliSeconds = 400;
            var token = await WaitForTokenAsync(session.PublicIdentifier, session.SessionToken, pollingIntervalInMilliSeconds, maxLoginTimeInSeconds);

            if (!string.IsNullOrEmpty(token))
            {
                TokenRetrieved?.Invoke(token);
            }
        }
Exemplo n.º 2
0
        private async void LoginButtonClick(object sender, RoutedEventArgs e)
        {
            if (TokenRetrieved == null)
            {
                throw new FieldAccessException($"No handler set for {nameof(TokenRetrieved)} event.");
            }

            var tokenRequest = new TokenRequest
            {
                Email    = UserNameTextBox.Text,
                Password = PasswordBox.Password
            };

            GoIntoLoadingState();

            try
            {
                var tokenResponse =
                    await _httpHandler.PostAsJsonAsync <TokenRequest, TokenResponse>("api/auth/token",
                                                                                     tokenRequest);

                TokenRetrieved.Invoke(tokenResponse.Token);

                Close();
            }
            catch (HttpRequestException requestException)
            {
                MessageTextBlock.Text = string.IsNullOrEmpty(requestException.Message) ? "Unknown error." : requestException.Message;
            }
            catch (HttpResponseException responseException)
            {
                var message = responseException.Message;
                if (string.IsNullOrEmpty(message) && responseException.ResponseStatusCode == HttpStatusCode.Unauthorized)
                {
                    message = "Invalid email / password combination.";
                }
                MessageTextBlock.Text = string.IsNullOrEmpty(message) ? "Unknown error." : message;
            }

            ComeOutOfLoadingState();
        }