Egloos | Log-in


[C#] 윈도우즈 프로그램에서 로그인 처리 하기

윈도우즈 폼 프로그램에서 사용자 아이디와 패스워드 확인 처리를 하기 위하여,
메인 폼의 Load 이벤트에 아래와 같이 추가한다.

        private void Form1_Load(object sender, EventArgs e)
        {
            Login login_form1 = new Login();

            if (login_form1.ShowDialog() != DialogResult.OK) // 사용자 아이디, 패스워드 확인이 실패할 경우 프로그램 종료
                this.Close();
        }

아래와 같이 로그인 처리를 할 새로운 폼(class Login)을 프로젝트에 추가한다.

Login 버튼(Button1)의 클릭 이벤트는 아래와 같다.

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

위 코드 중에서 아이디,패스워드를 확인하는 Authenticate() 함수는 아래와 같이 구현된다.

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

이 글과 관련있는 글을 자동검색한 결과입니다 [?]

by 헤이즐넛 | 2009/06/12 13:37 | 프로그래밍 | 트랙백 | 덧글(2)

트랙백 주소 : http://hazelstyle.egloos.com/tb/4978143
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]
Commented by 슈퍼컴 at 2009/08/26 20:31
헐.. 요즘 SQL Query 공부하고 있는데요..// pwdcompare 부분을 잘 모르겠습니다..

주석처리나 설명을 좀 덧붙여 주신다면 공부하는데 많은 도움이 될 거 같습니다.

해주시면 감사해용~
Commented at 2009/11/14 23:21
비공개 덧글입니다.

:         :

:

비공개 덧글

◀ 이전 페이지          다음 페이지 ▶