2017-02-03 53 views
1

我在進入商店頁面之前創建了應用程序,然後需要先登錄。所以先檢查一下。當它登錄時,它可以直接導航到商店頁面。但是如果沒有登錄,用戶必須登錄並點擊登錄按鈕後,它將導航到「商店」頁面。同樣,如果你想瀏覽到「Koleksi」頁面。登錄錯誤時輸入密碼錯誤

我有一個問題,即使密碼錯誤仍然可以登錄並領導Store Page或「Koleksi」頁面。當我添加導航到商店頁面或「Koleksi」頁面的功能時,會發生這種情況。在添加功能之前,如果密碼錯誤,則無法登錄。 XAML:

<Image x:Name="store" HorizontalAlignment="Center" Width="150" Height="150" Margin="20,0,0,0" VerticalAlignment="Center" Tapped="store_Tapped" Source="images/new icon/BSE-book-icon-store.png"/> 
       <Image x:Name="bukukoleksi" HorizontalAlignment="Center" Width="150" Height="150" Margin="20,0,0,0" VerticalAlignment="Center" Tapped="koleksi_Tapped" Source="images/new icon/bse-icon300-w8-KOLEKSI.png"/> 

<callisto:CustomDialog x:FieldModifier="public" x:Name="LoginDialog" 
         Title="Login" 
         Background="#FFFCDAA2" BackButtonVisibility="Collapsed" Grid.Row="0" Grid.RowSpan="2"> 
      <StackPanel x:Name="loginBox"> 
       <TextBlock x:Name="spacerLogin" VerticalAlignment="Top" Style="{StaticResource TitleTextBlockStyle}" Width="300" Height="50" Text=""/> 
       <Grid x:Name="loginEnter"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="auto"/> 
         <ColumnDefinition Width="*"/> 
        </Grid.ColumnDefinitions> 

        <Grid.RowDefinitions> 
         <RowDefinition Height="auto"/> 
         <RowDefinition Height="auto"/> 
         <RowDefinition Height="auto"/> 
         <RowDefinition Height="auto"/> 
         <RowDefinition Height="auto"/> 
        </Grid.RowDefinitions> 

        <TextBlock x:Name="FailedMessage" FontSize="17" Text="Login failed!" Foreground="Red" Visibility="Collapsed" Grid.Row="0" Grid.ColumnSpan="2"/> 
        <TextBlock x:Name="email" Text="Email:" Grid.Column="0" Grid.Row="1" Margin="0,10,0,0" FontSize="18" Height="40" VerticalAlignment="Center" Foreground="#FFCA6402"/> 
        <TextBox x:Name="emailBox" PlaceholderText="Email" FontSize="17" Grid.Column="1" Grid.Row="1" Margin="10,10,0,0" Height="40" VerticalAlignment="Top"/> 
        <TextBlock x:Name="password" Text="Password:" Grid.Column="0" Grid.Row="2" FontSize="18" Height="40" Margin="0,10,0,0" VerticalAlignment="Center" Foreground="#FFCA6402"/> 
        <PasswordBox x:Name="passwordBox" Grid.Column="1" Grid.Row="2" PlaceholderText="Password" FontSize="18" Height="40" Margin="10,10,0,0"/> 
        <StackPanel Grid.Row="3" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Center"> 
         <Button x:Name="loginBtn" Background="#FFCA6402" Content="Login" Margin="0,20,0,0" FontWeight="SemiBold" Click="loginBtn_Click" BorderBrush="Black" BorderThickness="1"/> 
         <Button x:Name="cancelLoginBtn" Background="#FFCA6402" Content="Batal" Margin="20,20,0,0" FontWeight="SemiBold" Click="cancelLoginBtn_Click" BorderBrush="Black" BorderThickness="1"/> 
        </StackPanel> 
        <ProgressRing x:Name="loadingLogin" Grid.RowSpan="4" Grid.ColumnSpan="2" Width="50" Height="50" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#FFCA6402" IsActive="False"/> 
       </Grid> 

代碼:

bool isstoreTapped = false; 
    bool iskoleksiTapped = false; 
    private void loginBtn_Click(object sender, RoutedEventArgs e) 
    { 
     loadingLogin.IsActive = true; 
     FailedMessage.Visibility = Visibility.Collapsed; 
     ProsesLogin(); 
     if(isstoreTapped==true) 
     { 
      this.Frame.Navigate(typeof(Store)); 
     } 
     if(iskoleksiTapped==true) 
     { 
      this.Frame.Navigate(typeof(koleksibuku.KolesiPage)); 
     } 

    } 

private async void ProsesLogin() 
     { 
      FailedMessage.Visibility = Visibility.Collapsed; 
      loadingLogin.IsActive = true; 
      try 
      { 
       var filter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter(); 
       filter.ServerCredential = new Windows.Security.Credentials.PasswordCredential("BSE_Win10(1)", "mahonidatastream", "Maho1019"); 
       var client = new Windows.Web.Http.HttpClient(filter); 
       string urlPath = "https://...m/user/auth"; 
       var values = new List<KeyValuePair<string, string>> 
       { 
        new KeyValuePair<string, string>("email", emailBox.Text), 
        new KeyValuePair<string, string>("password",passwordBox.Password) 
       }; 
       var response = await client.PostAsync(new Uri(urlPath), new Windows.Web.Http.HttpFormUrlEncodedContent(values)); 
       response.EnsureSuccessStatusCode(); 

       if (!response.IsSuccessStatusCode) 
       { 
        //RequestException(); 
        loadingLogin.IsActive = false; 
       } 

       string jsonText = await response.Content.ReadAsStringAsync(); 

       JsonObject jsonObject = JsonObject.Parse(jsonText); 
       bool error = jsonObject["error"].GetBoolean(); 
       //string message = jsonObject["message"].GetString(); 
       if (error == false) 
       { 
        LoginDialog.IsOpen = false; 
        FailedMessage.Visibility = Visibility.Collapsed; 
        ((App)(App.Current)).UserName = emailBox.Text; 
        loadingLogin.IsActive = false; 
       } 
       else 
       { 
        FailedMessage.Visibility = Visibility.Visible; 
        FailedMessage.Text = "Email atau password tidak sesuai/belum terdaftar"; 
        loadingLogin.IsActive = false; 
       } 

      } 
      catch (HttpRequestException ex) 
      { 
       //ConnectionException(); 
       loadingLogin.IsActive = false; 
      } 
     } 

    private void store_Tapped(object sender, TappedRoutedEventArgs e) 
    { 
     if (((App)(App.Current)).UserName == "Sign in to your account") 
     { 
      LoginDialog.IsOpen = true; 
      loginDetail.Visibility = Visibility.Collapsed; 
      loginEnter.Visibility = Visibility.Visible; 
      emailBox.Text = ""; 
      passwordBox.Password = ""; 
      isstoreTapped = true; 
      iskoleksiTapped = false; 
     } 
     else 
     { 
      this.Frame.Navigate(typeof(Store)); 
     } 
    } 

    private void koleksi_Tapped(object sender, TappedRoutedEventArgs e) 
    { 
     if (((App)(App.Current)).UserName == "Sign in to your account") 
     { 
      LoginDialog.IsOpen = true; 
      loginDetail.Visibility = Visibility.Collapsed; 
      loginEnter.Visibility = Visibility.Visible; 
      emailBox.Text = ""; 
      passwordBox.Password = ""; 
      iskoleksiTapped = true; 
      isstoreTapped = false; 
     } 
     else 
     { 
      this.Frame.Navigate(typeof(koleksibuku.KolesiPage)); 
     } 
    } 

回答

0

既然你已經有了導航標誌,你可以將它們設置爲假也時登錄失敗。在這種情況下,登錄失敗時將無法導航。例如:

//Your code 
    catch (HttpRequestException ex) 
    { 
     //ConnectionException(); 
     loadingLogin.IsActive = false; 
    } 
    //Add following code 
    if (loadingLogin.IsActive == false) 
    { 
     isstoreTapped = false; 
     iskoleksiTapped = false; 
    } 

添加此代碼塊到最後的ProcessLogin()方法。

+0

我添加了代碼: if(loadingLogin.IsActive == false) { isstoreTapped = false; iskoleksiTapped = false; (); } on ProgressLogin();但是當點擊storeBtn並輸入您的電子郵件和密碼時,即使輸入了密碼仍然可以導致Store Page。 – Rose