简体   繁体   English

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

[英]C# SQL Login that uses somekind of authentication

I'm in the need of some help probably because I can't express very well what I want on google. 我需要一些帮助,可能是因为我无法很好地表达我在Google上想要的东西。

    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();
    }

After the "Login Sucessfull" message box I wanted to make something like if permission = Admin then opens the AdminMenu else opens the WorkerMenu. 在“ Login Sucessfull”(登录成功)消息框之后,我想进行以下操作:ifPermission = Admin然后打开AdminMenu,否则打开WorkerMenu。

I think it's possible but not sure since I can't get my mind over it. 我认为这是可能的,但不确定,因为我无法确定。

I would recommend you to learn, how the Auth works on the sample ASP.Net application in Visual Studio (it would be the same in MVC and WinForms). 我建议您学习Auth如何在Visual Studio中的示例ASP.Net应用程序上工作(在MVC和WinForms中是相同的)。 Then you could use that approach and similar DB in your application. 然后,您可以在应用程序中使用该方法和类似的数据库。 It is really common functionality nowadays so there is no need to invent anything here. 如今,它确实是常见的功能,因此无需在此处进行任何发明。

Generally, it shouldn't be possible to store duplicated logins (and of cause login+password) in your database. 通常,应该不可能在数据库中存储重复的登录名(以及导致登录名+密码的原因)。 You should use constraints for it. 您应该为此使用约束。

For the logic below you should use Roles. 对于以下逻辑,应使用“角色”。 For instance, you could use Users-UsersInRoles-Roles DB structure. 例如,您可以使用Users-UsersInRoles-Roles DB结构。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM