我通常會創建一個像這樣的新窗體。
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();
}
}
}
然後在您的MainForm做到這一點:
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
,我只想把它變成一個存儲過程。但無論如何,這不是最好的方式進行身份驗證,但它的工作解決方案至少讓你去我希望
我明白。我有用戶數據庫創建。如果用戶存在並且密碼正確,我將只創建這些數據的驗證並顯示儀表板窗體。你怎麼看? 感謝您的回覆,順便說一下。抱歉我的怪胎英語。 :) –