在WPF中进行登录验证后导航到用户控件?

时间:2015-01-01 05:48:23

标签: c# wpf xaml user-controls expression-blend

我有一个Login UserControl和一个ModuleHomeScreen UserControl。

我的登录UserControl有一个用户名和密码TextBox和一个LoginButton。 当用户点击LoginButton时,他的凭据将从数据库中验证;然后他必须导航到ModuleHomeScreen UserControl。

UserCredentials验证工作正常。 MainWindow有一个NavigationFrame,我正在切换UserControl。

现在我的问题是: 如果没有填写用户凭据,当用户单击登录按钮时,他仍然会导航到Dashboard UserControl。 即使导航代码存在一些问题!

如何首先验证登录凭据,如果成功,则导航到不同的UserControl?

这是我附上的代码:

Login.xaml

    public void loginButton_Click(object sender, EventArgs e)
    {
        if (LoginUserName.Text != "" && LoginPassword.Password != "")
        {
            NpgsqlConnection con = new NpgsqlConnection("Server=localhost;Port=1234;UserID=postgres;Password=root;Database=postgres");
            con.Open();
            String username = LoginUserName.Text.Trim();
            String encPassword = TripleDESCrypto.Encrypt(LoginPassword.Password.Trim(),true);
            String query = "SELECT * FROM login where username= '" + username + "' and password = '" + encPassword + "';";
            NpgsqlCommand cmd = new NpgsqlCommand(query, con);
            NpgsqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
               // ServiceContainer.GetService<INavigationService>().Navigate("ModuleHomeScreen", null, this);
                LoginErrorMessage.Text = "Login Successfull";
                MainWindow appWindow = new MainWindow();
                ModuleHomeScreen appScreen = new ModuleHomeScreen();
                appWindow.MainWindowNavigationFrame.Navigate(appScreen,null);
            }
            else
            {
                LoginErrorMessage.Text = "Invalid Login Credentials.";
            }
        }
        else
        {
            LoginErrorMessage.Text = "Please enter all fields !";
        }
    }

1 个答案:

答案 0 :(得分:0)

试试这个:

    public void loginButton_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(LoginUserName.Text))
        {
            ////Password could be empty for some user's
            if (LoginPassword.Password == null)
                LoginPassword.Password = string.Empty;

            NpgsqlConnection con = new NpgsqlConnection("Server=localhost;Port=1234;UserID=postgres;Password=root;Database=postgres");
            con.Open();
            String username = LoginUserName.Text.Trim();
            String encPassword = TripleDESCrypto.Encrypt(LoginPassword.Password.Trim(), true);
            String query = "SELECT count(*) FROM login where username= '" + username + "' and password = '" + encPassword + "';";
            NpgsqlCommand cmd = new NpgsqlCommand(query, con);
            int? dr = cmd.ExecuteScalar() as int?;
            if (dr.HasValue && dr.Value > 0)
            {
                // ServiceContainer.GetService<INavigationService>().Navigate("ModuleHomeScreen", null, this);
                LoginErrorMessage.Text = "Login Successfull";
                MainWindow appWindow = new MainWindow();
                ModuleHomeScreen appScreen = new ModuleHomeScreen();
                appWindow.MainWindowNavigationFrame.Navigate(appScreen, null);
            }
            else
            {
                LoginErrorMessage.Text = "Invalid Login Credentials.";
            }
        }
        else
        {
            LoginErrorMessage.Text = "Please enter user name !";
        }
    }