locked
[UWP] Login, change password, logout RRS feed

  • Question

  • I have a button which when pressed will go to the login page. If you have not logged in, the user must enter your email and password. Meanwhile, if already login, then the user can change the password or logout. But I have a problem, that when I click the button logot, did not happen.

    Code:

    <Button x:Name="login1Btn" BorderBrush="{x:Null}" Foreground="White" Height="70" Width="auto" Click="login1Btn_Click" Grid.Column="3" Margin="20,0,0,0" VerticalAlignment="Center" Background="{x:Null}" Content="Sign In" FontSize="18" FontWeight="SemiBold"/>
    
    <callisto:CustomDialog x:FieldModifier="public" x:Name="LoginDialog" 
                           Title="Login"
                           Background="#FFECE7E3" 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"/>
                        </Grid.RowDefinitions>
    
                        <TextBlock x:Name="email" Text="Email:" Grid.Column="0" Grid.Row="0" FontSize="18" Height="30"/>
                        <TextBox x:Name="emailBox" PlaceholderText="Email" FontSize="18" Grid.Column="1" Grid.Row="0" Height="30" Margin="10,0,0,0" VerticalAlignment="Top"/>
                        <TextBlock x:Name="password" Text="Password:" Grid.Column="0" Grid.Row="1" FontSize="18" Height="30" Margin="0,10,0,0"/>
                        <PasswordBox x:Name="passwordBox" Grid.Column="1" Grid.Row="1" PlaceholderText="Password" FontSize="18" Height="30" Margin="10,10,0,0"/>
                        <Button x:Name="loginBtn" Background="#FFFFA429" Content="Login" Margin="0,20,0,0" Grid.Row="2" Grid.Column="0" Click="loginBtn_Click"/>
                        <Button x:Name="cancelLoginBtn" Background="#FFFFA429" Content="Cancel" Margin="0,20,0,0" Grid.Row="2" Grid.Column="1" Click="cancelLoginBtn_Click"/>
                        <ProgressRing x:Name="loadingLogin" Width="50" Height="50" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#FF7D350D" IsActive="False"/>
                    </Grid>
    
                    <Grid x:Name="loginDetail" Visibility="Collapsed">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto"/>
                            <RowDefinition Height="auto"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="auto"/>
                            <ColumnDefinition Width="auto" />
                            <ColumnDefinition Width="auto" />
                        </Grid.ColumnDefinitions>
                        <TextBlock x:Name="loginText" FontSize="18" Grid.ColumnSpan="3" Grid.Row="0"/>
                        <Button x:Name="changePBtn" Background="#FFFFA429" Content="Change Password" Margin="0,10,0,0" Grid.Row="1" Grid.Column="0" Click="changePBtn_Click"/>
                        <Button x:Name="logoutBtn" Background="#FFFFA429" Content="Logout" Margin="10,10,0,0" Grid.Row="1" Grid.Column="1" Click="logoutBtn_Click"/>
                        <Button x:Name="closeBtn" Background="#FFFFA429" Content="Close" Margin="10,10,0,0" Grid.Row="1" Grid.Column="2" Click="closeBtn_Click"/>
                    </Grid>
                </StackPanel>
            </callisto:CustomDialog>
    
            <callisto:CustomDialog x:FieldModifier="public" x:Name="ChangePasswordDetail" 
                           Title="Change Password"
                           Background="#FFECE7E3" BackButtonVisibility="Collapsed" Grid.Row="0" Grid.RowSpan="2">
                <StackPanel x:Name="changePasswordBox">
                    <TextBlock x:Name="spaceChangePassword" VerticalAlignment="Top" Style="{StaticResource TitleTextBlockStyle}" Width="300" Height="50" Text=""/>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto"/>
                            <RowDefinition Height="auto"/>
                            <RowDefinition Height="auto"/>
                            <RowDefinition Height="auto"/>
                        </Grid.RowDefinitions>
    
                        <!--<TextBlock x:Name="oldPassword" Text="Old Password" Grid.Column="0" Grid.Row="0" FontSize="18"/>
                        <PasswordBox x:Name="oldPasswordBox" PlaceholderText="Old Password" FontSize="18" Grid.Column="1" Grid.Row="0" Margin="10,0,0,0"/>-->
                        <TextBlock x:Name="newPassword" Text="New Password:" Grid.Column="0" Grid.Row="1" FontSize="18" Margin="0,10,0,0"/>
                        <PasswordBox x:Name="newPasswordBox" Grid.Column="1" Grid.Row="1" PlaceholderText="Password" Margin="10,10,0,0"/>
                        <TextBlock x:Name="confirmPassword" Text="Confirm Password:" Grid.Column="0" Grid.Row="2" FontSize="18" Margin="0,10,0,0"/>
                        <PasswordBox x:Name="confirmPasswordBox" Grid.Column="1" Grid.Row="2" PlaceholderText="Confirm Password" Margin="10,10,0,0"/>
                        <Button x:Name="changeBtn" Background="#FFFFA429" Content="Change Password" Margin="0,10,0,0" Grid.Row="3" Grid.Column="0" Click="changeBtn_Click"/>
                        <Button x:Name="cancelChangeBtn" Background="#FFFFA429" Content="Cancel" Margin="0,10,0,0" Grid.Row="3" Grid.Column="1" Click="cancelChangeBtn_Click"/>
                    </Grid>
                </StackPanel>
            </callisto:CustomDialog>

    private void login1Btn_Click(object sender, RoutedEventArgs e)
            {

    if (((App)(App.Current)).UserName == "Sign In") { LoginDialog.IsOpen = true; loginDetail.Visibility = Visibility.Collapsed; loginEnter.Visibility = Visibility.Visible; emailBox.Text = ""; passwordBox.Password = ""; } else { LoginDialog.IsOpen = true; loginText.Text = "Sign in as " + emailBox.Text; loginDetail.Visibility = Visibility.Visible; loginEnter.Visibility = Visibility.Collapsed; } private async void loginBtn_Click(object sender, RoutedEventArgs e) { loadingLogin.IsActive = true;

    ProsesLogin(); } private async void ProsesLogin() { busyindicator.Visibility = Visibility.Visible; try { var filter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter(); filter.ServerCredential = new Windows.Security.Credentials.PasswordCredential("Intisari_Win10", "user", "password"); var client = new Windows.Web.Http.HttpClient(filter); string urlPath = "website"; var values = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("device_id","win"), new KeyValuePair<string, string>("username", emailBox.Text), new KeyValuePair<string, string>("passww",passwordBox.Password) }; var response = await client.PostAsync(new Uri(urlPath), new Windows.Web.Http.HttpFormUrlEncodedContent(values)); if (!response.IsSuccessStatusCode) { RequestException(); busyindicator.Visibility = Visibility.Collapsed; } string jsonText = await response.Content.ReadAsStringAsync(); JsonObject jsonObject = JsonObject.Parse(jsonText); bool found = jsonObject["status"].GetBoolean(); string message = jsonObject["message"].GetString(); if (found == true) { LoginDialog.IsOpen = false; ((App)(App.Current)).UserName = emailBox.Text; loadingLogin.IsActive = false; } else { loadingLogin.IsActive = false; } } catch (HttpRequestException ex) { ConnectionException(); busyindicator.Visibility = Visibility.Collapsed; } } private void cancelLoginBtn_Click(object sender, RoutedEventArgs e) { LoginDialog.IsOpen = false; } private async void changeBtn_Click(object sender, RoutedEventArgs e) { if (newPasswordBox.Password == confirmPasswordBox.Password) { ChangePassword(); } else { MessageDialog messageDialog; messageDialog = new MessageDialog("Password yang Anda masukkan di New Password dan di Confirm Password tidak sama","Perubahan Password Gagal"); messageDialog.Commands.Add(new UICommand("Coba Lagi" , (command)=> { })); await messageDialog.ShowAsync(); } } private async void ChangePassword() { try { var filter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter(); filter.ServerCredential = new Windows.Security.Credentials.PasswordCredential("Intisari_Win10", "user", "password"); var client = new Windows.Web.Http.HttpClient(filter); string urlPath = "website"; var httpClient = new HttpClient(new HttpClientHandler()); var values = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("device_id", "win"), new KeyValuePair<string, string>("email", ((App)(App.Current)).UserName), new KeyValuePair<string, string>("passww",newPasswordBox.Password), }; var response = await client.PostAsync(new Uri(urlPath), new Windows.Web.Http.HttpFormUrlEncodedContent(values)); response.EnsureSuccessStatusCode(); if (!response.IsSuccessStatusCode) { RequestException(); } string jsonText = await response.Content.ReadAsStringAsync(); JsonObject jsonObject = JsonObject.Parse(jsonText); bool found = jsonObject["status"].GetBoolean(); string message = jsonObject["message"].GetString(); if (found == true) { ChangePasswordDetail.IsOpen = false; } else { MessageDialog messageDialog; messageDialog = new MessageDialog("Password yang Anda masukkan di New Password dan di Confirm Password tidak sama", "Perubahan Password Gagal"); messageDialog.Commands.Add(new UICommand("Coba Lagi", (command) => { })); await messageDialog.ShowAsync(); //ChangePasswordFailedMessage.Visibility = Visibility.Visible; //busyindicator1.Visibility = Visibility.Collapsed; } } catch (HttpRequestException ex) { ConnectionException(); //busyindicator1.Visibility = Visibility.Collapsed; } } private void cancelChangeBtn_Click(object sender, RoutedEventArgs e) { ChangePasswordDetail.IsOpen = false; } private void changePBtn_Click(object sender, RoutedEventArgs e) { ChangePasswordDetail.IsOpen = true; newPasswordBox.Password = ""; confirmPasswordBox.Password = ""; } private void logoutBtn_Click(object sender, RoutedEventArgs e) { ((App)(App.Current)).UserName = "Sign in"; accountBtn.Content = ((App)(App.Current)).UserName; LoginDialog.IsOpen = false; } private void closeBtn_Click(object sender, RoutedEventArgs e) { LoginDialog.IsOpen = false; loginDetail.Visibility = Visibility.Collapsed; }

    How to handle it?



    • Edited by Priscillia Wednesday, March 16, 2016 6:35 AM
    Wednesday, March 16, 2016 6:31 AM

Answers

  • "Sign In" != "Sign in"


    http://pauliom.wordpress.com

    Line 110 of your MainPage.cs

    ((App)(App.Current)).UserName = "Sign in";

    change that to

    ((App)(App.Current)).UserName = "Sign In";

    then your code should work already.

    What is the differences of 

    ((App)(App.Current)).UserName = "Sign in";

    and 

    ((App)(App.Current)).UserName = "Sign In";

    ?

    For:

    "Sign In" != "Sign in"

    put where?

    Pkr2000 state that was the error.. that "Sign In" != "Sign in"

    Cause in line 110 under your mainpage.xaml.cs shows as "Sign in" (small i) which is why it didn't pass the condition in line 34. Where you state  if (((App)(App.Current)).UserName == "Sign In") (capital I).

    So to fix this.. Just change your "Sign in" to "Sign In" at line 110 of your mainpage.xaml.cs. Then you should get your result.

    • Marked as answer by Priscillia Wednesday, March 23, 2016 7:24 AM
    Wednesday, March 23, 2016 4:08 AM

All replies

  • Hello Priscillia,

    With your current code and description, it is hard to tell what the caused reason for your encountered scenario(in actual, I do not quite understand what you mean about “that when I click the button logot, did not happen.”, what does not happen?).

    So for providing a better support, I recommend you could share a repro project and detail steps to reproduce the scenario on your side, you could upload your project to onedrive, we then could download it and make a test with it.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Friday, March 18, 2016 2:59 AM
  • Steps:
    1. click the login button
    2. enter your username and password and press login
    3. Click the login button again
    4. click the logout button (unsuccessful)

    How does that account can be log out? Because I try to logout, but is unsuccessful

    Link project: 

    https://onedrive.live.com/redir?resid=D40E9E24C5BFA2EA!13340&authkey=!AEzd6fDaiI-nsgs&ithint=folder%2csln

    Note for login:

    username : info@mahoni.com

    password : 12345

    Friday, March 18, 2016 9:15 AM
  • But what is unsuccessful? It doesn't run your code in the logout button or...?

    http://pauliom.wordpress.com

    Saturday, March 19, 2016 10:32 AM
  • But what is unsuccessful? It doesn't run your code in the logout button or...?

    http://pauliom.wordpress.com

    yes, it doesn't run my code in the logoout button. 

    How to handle it?

    Monday, March 21, 2016 1:18 AM
  • Hello Priscillia,

    >>How does that account can be log out? Because I try to logout, but is unsuccessful

    If you save the login information on the server side, you should post a request to server side and tell the server to logout that user, for example, clear the session.

    The windows runtime does not have the session concept in web browser, but you could do the same thing with the app local setting, that is to maintain a directory object to store the users that have logon ever. When a user opens the app and after login, storing the user with a flag(for example, “IsLogin” to be true) to the directory object indicate the user has logon, you could store the directory object into the app local setting. Everytime open the app, you could check if there are users have logon, if have, then shows that users name(I suspect at the same time, there is only one user could login on a same machine because if I do not think it is reasonable that there are multi accounts could logon to a same machine in a same app).

    After the user choices to logout, you could send a post to your server side to tell the user that he wants to logout, after the server done that and gave a response that indicates the user has logout from the server side, you could then set the flag store in the directory object to be false, for example, “IsLogin” to be false.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Monday, March 21, 2016 9:45 AM
  • Here is my code:

     private void login1Btn_Click(object sender, RoutedEventArgs e)
            {
                if (((App)(App.Current)).UserName == "Sign In")
                {
                    LoginDialog.IsOpen = true;
                    loginDetail.Visibility = Visibility.Collapsed;
                    loginEnter.Visibility = Visibility.Visible;
                    emailBox.Text = "";
                    passwordBox.Password = "";
                    login1Btn.Visibility = Visibility.Visible;
                    //accountBtn.Visibility = Visibility.Collapsed;
                }
                else
                {
                    LoginDialog.IsOpen = true;
                    loginText.Text = "Sign in as " + emailBox.Text;
                    loginDetail.Visibility = Visibility.Visible;
                    loginEnter.Visibility = Visibility.Collapsed;
                    //accountBtn.Visibility = Visibility.Visible;
                    login1Btn.Visibility = Visibility.Visible;
                }
            }
    
    private async void loginBtn_Click(object sender, RoutedEventArgs e)
            {
                loadingLogin.IsActive = true;
                ProsesLogin();
                AccountBtn.Text = "Sign in as " + emailBox.Text;
            }
    
    
    private async void ProsesLogin()
            {
                //FailedMessage.Visibility = Visibility.Collapsed;
                busyindicator.Visibility = Visibility.Visible;
                try
                {
                    var filter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
                    filter.ServerCredential = new Windows.Security.Credentials.PasswordCredential("App", "username", "password");
                    var client = new Windows.Web.Http.HttpClient(filter);
                    string urlPath = "website";
                    //var httpClient = new HttpClient(new HttpClientHandler());
                    var values = new List<KeyValuePair<string, string>>
                    {
                        new KeyValuePair<string, string>("device_id","win"),
                        new KeyValuePair<string, string>("username", emailBox.Text),
                        new KeyValuePair<string, string>("passww",passwordBox.Password)
                    };
                    var response = await client.PostAsync(new Uri(urlPath), new Windows.Web.Http.HttpFormUrlEncodedContent(values));
    
                    if (!response.IsSuccessStatusCode)
                    {
                        RequestException();
                        busyindicator.Visibility = Visibility.Collapsed;
                    }
    
                    string jsonText = await response.Content.ReadAsStringAsync();
    
                    JsonObject jsonObject = JsonObject.Parse(jsonText);
                    bool found = jsonObject["status"].GetBoolean();
                    string message = jsonObject["message"].GetString();
                    if (found == true)
                    {
                        LoginDialog.IsOpen = false;
                        //FailedMessage.Visibility = Visibility.Collapsed;
                        ((App)(App.Current)).UserName = emailBox.Text;
                        //login.Content = "Welcome, " + ((App)(App.Current)).UserName;
                        //logout.Visibility = Visibility.Visible;
                        loadingLogin.IsActive = false;
                    }
                    else
                    {
                        //FailedMessage.Visibility = Visibility.Visible;
                        loadingLogin.IsActive = false;
                    }
    
                }
                catch (HttpRequestException ex)
                {
                    ConnectionException();
                    busyindicator.Visibility = Visibility.Collapsed;
                }
            }
    
    private void logoutBtn_Click(object sender, RoutedEventArgs e)
            {
                ((App)(App.Current)).UserName = "Sign in";
                login1Btn.Content = ((App)(App.Current)).UserName;
                LoginDialog.IsOpen = false;
            }

    App.cs code:

    public string UserName { set; get; }
    
    public App()
            {
                Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(
                    Microsoft.ApplicationInsights.WindowsCollectors.Metadata |
                    Microsoft.ApplicationInsights.WindowsCollectors.Session);
                this.InitializeComponent();
                this.Suspending += OnSuspending;
                ((App)(App.Current)).UserName = "Sign In";
            }

    But,  it doesn't run my code in the logoout button (cannot logout the account)

    Please help me

    Tuesday, March 22, 2016 1:33 AM
  • So you are saying that then you are showing the LoginDialog the logout button click event does not run the logout code? Have you put a breakpoint in that event handler?

    http://pauliom.wordpress.com

    Tuesday, March 22, 2016 6:36 AM
  • yes, when click the logout button, logout button click event does not run the logout code (the account did not want at log out).

    How to handle it?

    Is there a wrong code on logoutBtn_Click or on loginBtn_Clicked? 

    Tuesday, March 22, 2016 7:31 AM
  • Sorry, can you just confirm that the event handler doesn't fire. I'm not certain if you mean that the code doesn't run or that it doesn't do what you want. So can we first confirm that the event handler is not being called when you click the logout button *when* the logout dialog is showing?

    http://pauliom.wordpress.com

    Tuesday, March 22, 2016 8:13 AM
  • I mean when the logout button is clicked, the account can not log out.
    This sample it:

    https://onedrive.live.com/redir?resid=D40E9E24C5BFA2EA!13340&authkey=!AEzd6fDaiI-nsgs&ithint=folder%2csln

    Note for login:

    username : info@mahoni.com

    password : 12345

    Tuesday, March 22, 2016 9:25 AM
  • "Sign In" != "Sign in"


    http://pauliom.wordpress.com

    • Proposed as answer by Alex Jong Wednesday, March 23, 2016 2:47 AM
    Tuesday, March 22, 2016 9:42 PM
  • "Sign In" != "Sign in"


    http://pauliom.wordpress.com

    Line 110 of your MainPage.cs

    ((App)(App.Current)).UserName = "Sign in";

    change that to

    ((App)(App.Current)).UserName = "Sign In";

    then your code should work already.

    Wednesday, March 23, 2016 2:48 AM
  • "Sign In" != "Sign in"


    http://pauliom.wordpress.com

    Line 110 of your MainPage.cs

    ((App)(App.Current)).UserName = "Sign in";

    change that to

    ((App)(App.Current)).UserName = "Sign In";

    then your code should work already.

    What is the differences of 

    ((App)(App.Current)).UserName = "Sign in";

    and 

    ((App)(App.Current)).UserName = "Sign In";

    ?

    For:

    "Sign In" != "Sign in"

    put where?

    Wednesday, March 23, 2016 3:40 AM
  • "Sign In" != "Sign in"


    http://pauliom.wordpress.com

    Line 110 of your MainPage.cs

    ((App)(App.Current)).UserName = "Sign in";

    change that to

    ((App)(App.Current)).UserName = "Sign In";

    then your code should work already.

    What is the differences of 

    ((App)(App.Current)).UserName = "Sign in";

    and 

    ((App)(App.Current)).UserName = "Sign In";

    ?

    For:

    "Sign In" != "Sign in"

    put where?

    Pkr2000 state that was the error.. that "Sign In" != "Sign in"

    Cause in line 110 under your mainpage.xaml.cs shows as "Sign in" (small i) which is why it didn't pass the condition in line 34. Where you state  if (((App)(App.Current)).UserName == "Sign In") (capital I).

    So to fix this.. Just change your "Sign in" to "Sign In" at line 110 of your mainpage.xaml.cs. Then you should get your result.

    • Marked as answer by Priscillia Wednesday, March 23, 2016 7:24 AM
    Wednesday, March 23, 2016 4:08 AM