C# WinForms应用程序的Windows窗体身份验证
我正在Visual Studio 2012上使用C#和.NET Framework 4.5创建一个Windows窗体应用程序 我现在想要创建一个登录表单,用户可以在其中输入一些用户名和密码(以前在数据库中创建),应用程序验证并登录用户。如果可能,使用“角色控制” 我试着在谷歌上搜索,但我没有找到与Windows窗体相关的内容,只是在ASP.NET上C# WinForms应用程序的Windows窗体身份验证,c#,winforms,authentication,.net-4.5,roles,C#,Winforms,Authentication,.net 4.5,Roles,我正在Visual Studio 2012上使用C#和.NET Framework 4.5创建一个Windows窗体应用程序 我现在想要创建一个登录表单,用户可以在其中输入一些用户名和密码(以前在数据库中创建),应用程序验证并登录用户。如果可能,使用“角色控制” 我试着在谷歌上搜索,但我没有找到与Windows窗体相关的内容,只是在ASP.NET上 .NET Framework是否有任何好的(正式的)解决方案来解决WinForms中的身份验证问题?没有。成员资格系统是Asp.NET的一部分,虽然
.NET Framework是否有任何好的(正式的)解决方案来解决WinForms中的身份验证问题?没有。成员资格系统是Asp.NET的一部分,虽然您可以在WinForms应用程序中使用它,但它不是很干净 如果数据库中已经有用户名和密码,那么最好的办法就是直接在认证系统中实现,除非您担心人们对代码进行反向工程。。。在这种情况下,它是一个更先进的事情,使其安全的反向工程 编辑:
微软确实有,但它确实是一个比你可能想要的更复杂的系统。我通常会创建一个类似这样的新表单
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