Computer
[C#] 로그인 처리 하기
창천(蒼天)
2013. 4. 9. 15:27
출처 : http://hazelstyle.egloos.com/4978143
private void button1_Click(object sender, EventArgs e)
{
if (textID.Text.Trim() == "" || textPwd.Text.Trim() == "")
{
if (MessageBox.Show("아이디와 암호를 입력해 주세요.", "아이디 입력 오류", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.Retry)
{
textID.Text = "";
textPwd.Text = "";
}
}
else
{
int res = Authenticate(textID.Text.Trim(), textPwd.Text.Trim());
if (res > 0)
{
this.DialogResult = DialogResult.OK;
}
else
{
switch (res)
{
case -1:
if (MessageBox.Show("아이디를 잘못 입력하였습니다.", "사용자 로그인 오류", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning) == DialogResult.Retry)
{
textID.Text = "";
textPwd.Text = "";
}
else
this.DialogResult = DialogResult.Cancel;
break;
case -2:
if (MessageBox.Show("암호를 잘못 입력하였습니다.", "사용자 로그인 오류", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning) == DialogResult.Retry)
{
textPwd.Text = "";
}
else
this.DialogResult = DialogResult.Cancel;
break;
default:
MessageBox.Show("ISM-DVS 시스템 오류입니다. 관리자에게 문의하세요.", "사용자 로그인 오류", MessageBoxButtons.OK, MessageBoxIcon.Warning);
this.DialogResult = DialogResult.Cancel;
break;
}
}
}
}
private int Authenticate (string id, string pwd)
{
string con_str = "server=127.0.0.1; database=xxx; user id = sa; password=1111;";
SqlConnection con = new SqlConnection(con_str);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select pwdcompare(@pwd,passwd), idx from users where userid = @id";
cmd.Parameters.Add("@id", SqlDbType.VarChar, 20);
cmd.Parameters.Add("@pwd", SqlDbType.VarChar, 50);
cmd.Parameters["@id"].Value = id;
cmd.Parameters["@pwd"].Value = pwd;
SqlDataReader reader = null;
try
{
con.Open();
reader = cmd.ExecuteReader();
if (reader.Read())
{
if ((int)reader[0] > 0)
return 1;
else
return -2; // 암호 오류
}
else
return -1; // 아이디 오류
}
catch
{
MessageBox.Show("데이터 베이스 접속 오류", "사용자 확인");
return -99;
}
finally
{
if (reader != null) reader.Close();
con.Close();
}
}