使用某种身份验证的C#SQL登录

本文关键字:C#SQL 登录 身份验证 | 更新日期: 2023-09-27 18:20:04

我需要一些帮助,可能是因为我不能很好地在谷歌上表达我想要的东西。

    private void LogInBt_Click(object sender, EventArgs e)
    {
        const string conString = @"Data Source=.'SQLEXPRESS;AttachDbFilename=H:'Jogos.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
        SqlConnection con = new SqlConnection(conString);
        con.Open();
        SqlCommand log = new SqlCommand("SELECT * FROM Funcionario WHERE [e-mail] ='" + textBox1.Text + "' and Password ='" + textBox2.Text + "'", con);
        SqlDataReader dr;
        dr = log.ExecuteReader();
        int count = 0;
        while (dr.Read())
        {
            count += 1;
        }
        if (count == 1)
        {
            MessageBox.Show("Login Succesfull");
            if ()
            {
                Form menu = new MenuPrincipalAdmin();
                menu.Show();
                this.Hide();
            }
            else
            {
                Form menu = new MenuPrincipalFunc();
                menu.Show();
                this.Hide();
            }
        }
        else if (count > 0)
            MessageBox.Show("Duplicate e-mail and password.");
        else
            MessageBox.Show("E-mail or Password invalid.");
        textBox1.Clear();
        textBox2.Clear();
        con.Close();
    }

在"登录成功"消息框之后,我想做一些类似于如果permission=Admin,然后打开AdminMenu,否则打开WorkerMenu的操作。

我认为这是可能的,但不确定,因为我无法集中注意力。

使用某种身份验证的C#SQL登录

我建议您学习Auth如何在Visual Studio中的示例ASP.Net应用程序上工作(在MVC和WinForms中也是如此)。然后,您可以在应用程序中使用这种方法和类似的DB。如今,它确实是一种常见的功能,所以没有必要在这里发明任何东西。

一般来说,不可能在数据库中存储重复的登录名(当然还有登录名+密码)。你应该使用约束。

对于下面的逻辑,您应该使用角色。例如,您可以使用UsersUsersInRoles角色数据库结构。

        if ()
        {
            Form menu = new MenuPrincipalAdmin();
            menu.Show();
            this.Hide();
        }
        else
        {
            Form menu = new MenuPrincipalFunc();
            menu.Show();
            this.Hide();
        }