Windows Forms身份验证用于WinForms应用程序

4

我正在使用C#和.NET Framework 4.5在Visual Studio 2012中创建Windows窗体应用程序。

现在,我想创建一个登录窗体,在此窗体上用户可以输入一些用户名和密码(之前在数据库中创建),应用程序会验证并登录用户。如果可能的话,还带有"角色控制"。

我尝试在Google上搜索,但是没有找到与Windows Forms相关的内容,只有ASP.NET的内容。

.NET Framework是否有任何好的(并且官方)解决方案来解决WinForms中的身份验证问题?

2个回答

3
不行。会员制系统是Asp.net的一部分,虽然你可能能够在winforms应用程序中使用它,但并不会很干净。如果您已经在数据库中拥有用户名和密码,则最好实现一个简单的身份验证系统,除非您担心人们反向工程代码...在这种情况下,使其安全防止反向工程是一件更高级的事情。编辑:Microsoft确实有Windows Identity Foundation,但它实际上比你想要的更复杂。

我明白了。我已经创建了用户数据库。我将验证这些数据并在用户存在且密码正确时显示仪表板表单。你觉得怎么样?顺便说一下,谢谢你的回复。对于我的糟糕英语,我很抱歉。 :) - Luiz Henrique

1
我通常会像这样创建一个新表单。
public partial class LoginForm : Form
{
    public bool letsGO = false;

    public LoginForm()
    {
        InitializeComponent();
        textUser.CharacterCasing = CharacterCasing.Upper;
    }

    public string UserName
    {
        get
        {
            return textUser.Text;
        }
    }

    private static DataTable LookupUser(string Username)
    {
        const string connStr = "Server=(local);" +
                            "Database=LabelPrinter;" +
                            "trusted_connection= true;" +
                            "integrated security= true;" +
                            "Connect Timeout=1000;";

        //"Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;";

        const string query = "Select password From dbo.UserTable (NOLOCK) Where UserName = @UserName";
        DataTable result = new DataTable();
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = Username;
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    result.Load(dr);
                }
            }
        }
        return result;
    }

    private void HoldButton()
    {
        if (string.IsNullOrEmpty(textUser.Text))
        {
            //Focus box before showing a message
            textUser.Focus();
            MessageBox.Show("Enter your username", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            //Focus again afterwards, sometimes people double click message boxes and select another control accidentally
            textUser.Focus();
            textPass.Clear();
            return;
        }
        else if (string.IsNullOrEmpty(textPass.Text))
        {
            textPass.Focus();
            MessageBox.Show("Enter your password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            textPass.Focus();
            return;

        }
        //OK they enter a user and pass, lets see if they can authenticate
        using (DataTable dt = LookupUser(textUser.Text))
        {
            if (dt.Rows.Count == 0)
            {
                textUser.Focus();
                MessageBox.Show("Invalid username.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                textUser.Focus();
                textUser.Clear();
                textPass.Clear();
                return;
            }
            else
            {
                string dbPassword = Convert.ToString(dt.Rows[0]["Password"]);
                string appPassword = Convert.ToString(textPass.Text); //we store the password as encrypted in the DB

                Console.WriteLine(string.Compare(dbPassword, appPassword));

                if (string.Compare(dbPassword, appPassword) == 0)
                {
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    //You may want to use the same error message so they can't tell which field they got wrong
                    textPass.Focus();
                    MessageBox.Show("Invalid Password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    textPass.Focus();
                    textPass.Clear();
                    return;
                }
            }
        }
    }

    private void textPass_KeyDown_1(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Return)
        {
            HoldButton();
        }
    }

    private void buttonLogin_Click(object sender, EventArgs e)
    {
        HoldButton();
    }

    private void textPass_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Return)
        {
            HoldButton();
        }
    }
}

然后在你的主表单中这样做:

public Form1(string userName)
{
    //this is incase a user has a particular setting in your form
    //so pass name into contructer
}

最后是:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        LoginForm fLogin = new LoginForm();

        if (fLogin.ShowDialog() == DialogResult.OK)
        {
            Application.Run(new Form1(fLogin.UserName));

        }
        else
        {
            Application.Exit();
        }

        //Application.Run(new Form1());
    }

希望这能给你一个大致的想法,虽然我相信有更好的方法来做到这一点,也请注意这并不是真正安全的前端。

希望这可以帮到你:

编辑:哦,在我忘记之前不要使用


Select password From dbo.UserTable (NOLOCK) Where UserName = @UserName 

我只是会将它放到一个存储过程中。但无论如何,这不是最好的身份验证方式,但至少是一种可行的解决方案,希望能让您继续前进。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接