示例#1
0
        private void ButtonLogin_OnClick(object sender, RoutedEventArgs e)
        {
            var loginMember = new LoginMember()
            {
                email    = this.Email.Text,
                password = this.Password.Password,
            };

            Dictionary <string, string> errors = loginMember.Validate();

            if (errors.Count == 0)
            {
                var httpClient          = new HttpClient();
                var dataToSend          = JsonConvert.SerializeObject(loginMember);
                var content             = new StringContent(dataToSend, Encoding.UTF8, "application/json");
                var response            = httpClient.PostAsync(LoginUrl, content).GetAwaiter().GetResult();
                var jsonContent         = response.Content.ReadAsStringAsync().Result;
                var responseLoginMember = JsonConvert.DeserializeObject <MemberCredential>(jsonContent);
                Debug.WriteLine(response);
                SaveTokenToFile(responseLoginMember);
            }
            else
            {
                if (errors.ContainsKey("email"))
                {
                    EmailError.Text       = errors["email"];
                    EmailError.Visibility = Visibility.Visible;
                }
                else
                {
                    EmailError.Visibility = Visibility.Collapsed;
                }

                if (errors.ContainsKey("password"))
                {
                    PwdError.Text       = errors["password"];
                    PwdError.Visibility = Visibility.Visible;
                }
                else
                {
                    PwdError.Visibility = Visibility.Collapsed;
                }
            }
        }
示例#2
0
        private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            var member = new LoginMember
            {
                email    = Email.Text,
                password = Password.Password,
            };

            Dictionary <string, string> errors = member.Validate();

            if (errors.Count == 0)
            {
                try
                {
                    var jsonResult = memberService.Login(member);
                    if (jsonResult.Contains("error"))
                    {
                        throw new Exception("Invalid email or password!");
                    }
                    var resMember = JsonConvert.DeserializeObject <LoginMember>(jsonResult);
                    var token     = resMember.token;

                    var sampleFile       = fileService.WriteIntoTxtFile(token);
                    var pathOfSampleFile = sampleFile.Path;
                    validateService.ValidateTrue();
                    this.NavigationCacheMode = NavigationCacheMode.Disabled;
                    this.Frame.Navigate(typeof(NavigationView));
                }
                catch (Exception exception)
                {
                    MessageDialog dialog = new MessageDialog(exception.Message);
                    await dialog.ShowAsync();
                }
            }
            else
            {
                validateService.ValidateFalse(EmailMessage, errors, "email");
                validateService.ValidateFalse(PasswordMessage, errors, "password");
            }
        }