2013년 9월 2일 월요일

C# 윈폼 로그인 구현하기

FrmLogin : 로그인 윈도우
MDIMain : 로그인후 나타나는 윈도우

MDIMain의 생성자에서 FrmLogin을 로딩하고 여기서 아이디와 비밀번호로 인증을 한 후 그 결과를 MDIMain으로 넘김으로써 인증처리는 하는 예제 입니다.
(로그인 후 로그인 창은 종료 됩니다.)

앞 강좌의 주소록에 포함되어 있지만 이 부분만 분리했습니다.

--------------------------------------------------------------

==============================================
FrmLogin.cs
==============================================


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;

namespace 주소록
{
        /// <summary>
        /// FrmLogin에 대한 요약 설명입니다.
        /// </summary>
        public class FrmLogin : System.Windows.Forms.Form
        {
                private System.Windows.Forms.Label label1;
                private System.Windows.Forms.Label label2;
                private System.Windows.Forms.Button button1;
                private System.Windows.Forms.TextBox txtID;
                private System.Windows.Forms.TextBox txtPWD;
                /// <summary>
                /// 필수 디자이너 변수입니다.
                /// </summary>
                private System.ComponentModel.Container components = null;

                public FrmLogin()
                {
                        //
                        // Windows Form 디자이너 지원에 필요합니다.
                        //
                        InitializeComponent();

                        //
                        // TODO: InitializeComponent를 호출한 다음 생성자 코드를 추가합니다.
                        //
                }

                /// <summary>
                /// 사용 중인 모든 리소스를 정리합니다.
                /// </summary>
                protected override void Dispose( bool disposing )
                {
                        if( disposing )
                        {
                                if(components != null)
                                {
                                        components.Dispose();
                                }
                        }
                        base.Dispose( disposing );
                }

                #region Windows Form Designer generated code
                /// <summary>
                /// 디자이너 지원에 필요한 메서드입니다.
                /// 이 메서드의 내용을 코드 편집기로 수정하지 마십시오.
                /// </summary>
                private void InitializeComponent()
                {
                        this.label1 = new System.Windows.Forms.Label();
                        this.label2 = new System.Windows.Forms.Label();
                        this.txtID = new System.Windows.Forms.TextBox();
                        this.txtPWD = new System.Windows.Forms.TextBox();
                        this.button1 = new System.Windows.Forms.Button();
                        this.SuspendLayout();
                        //
                        // label1
                        //
                        this.label1.AutoSize = true;
                        this.label1.Font = new System.Drawing.Font("굴림", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(129)));
                        this.label1.Location = new System.Drawing.Point(16, 16);
                        this.label1.Name = "label1";
                        this.label1.Size = new System.Drawing.Size(19, 15);
                        this.label1.TabIndex = 0;
                        this.label1.Text = "ID";
                        //
                        // label2
                        //
                        this.label2.AutoSize = true;
                        this.label2.Font = new System.Drawing.Font("굴림", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(129)));
                        this.label2.Location = new System.Drawing.Point(16, 40);
                        this.label2.Name = "label2";
                        this.label2.Size = new System.Drawing.Size(70, 15);
                        this.label2.TabIndex = 1;
                        this.label2.Text = "PassWord";
                        //
                        // txtID
                        //
                        this.txtID.Location = new System.Drawing.Point(104, 8);
                        this.txtID.Name = "txtID";
                        this.txtID.Size = new System.Drawing.Size(120, 21);
                        this.txtID.TabIndex = 2;
                        this.txtID.Text = "";
                        //
                        // txtPWD
                        //
                        this.txtPWD.Location = new System.Drawing.Point(104, 32);
                        this.txtPWD.Name = "txtPWD";
                        this.txtPWD.PasswordChar = '*';
                        this.txtPWD.Size = new System.Drawing.Size(120, 21);
                        this.txtPWD.TabIndex = 3;
                        this.txtPWD.Text = "";
                        //
                        // button1
                        //
                        this.button1.Location = new System.Drawing.Point(232, 8);
                        this.button1.Name = "button1";
                        this.button1.Size = new System.Drawing.Size(56, 48);
                        this.button1.TabIndex = 4;
                        this.button1.Text = "로그인";
                        this.button1.Click += new System.EventHandler(this.button1_Click);
                        //
                        // FrmLogin
                        //
                        this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
                        this.ClientSize = new System.Drawing.Size(304, 69);
                        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                                                                                  this.button1,
                                                                                                                                                  this.txtPWD,
                                                                                                                                                  this.txtID,
                                                                                                                                                  this.label2,
                                                                                                                                                  this.label1});
                        this.Name = "FrmLogin";
                        this.Text = "FrmLogin";
                        this.ResumeLayout(false);

                }
                #endregion

                [STAThread]
                static void Main()
                {
                        Application.Run(new FrmLogin());
                }

                private OleDbConnection LocalConn;


//---------------------------------------------//
//--------------------------------------------//
//------- 이 부분이 인증을 하는 부분 입니다. ----//
//---------------------------------------------//
//--------------------------------------------//
                private void button1_Click(object sender, System.EventArgs e)
                {
                        OleDbDataReader myReader;
                        string sql = null;
                        try
                        {
                                LocalConn = Common_DB.DBConnection();
                                LocalConn.Open();

                                if (txtID.Text == "" || txtPWD.Text == "")
                                {
                                        MessageBox.Show("ID 또는 Password를 입력 하세요...");
                                        return;
                                }

                                sql  = "select pwd from member ";
                                sql += " where id = " + "'" + txtID.Text + "'";

                                myReader = Common_DB.DataSelect(sql, LocalConn);

                                if (myReader.Read())
                                {
                                        if (txtPWD.Text != myReader["pwd"].ToString())
                                        {
                                                MessageBox.Show("Password가 맞지 않습니다...");
                                                return;
                                        }
                                }
                                else
                                {
                                        MessageBox.Show("등록되지 않은 ID 입니다.");
                                        return;
                                }
                               
                                //----------- ID가 PWD가 맞는 경우
                                MDIMain.loginOK = true;
                                //---------------------------------

                                this.Close();               
                        }
                        catch(Exception e1)
                        {
                                Log.WriteLine("FrmLogin", e1.ToString());
                                MessageBox.Show("FrmLogin", "로그인 오류! " + sql );
                        }
                }
        }
}



==============================================
MDIMain.cs
==============================================

아래는 MDIMain의 생성자 부분 입니다.

//----------------------  FOR LOGIN
                static FrmLogin login = null;
                internal static bool loginOK = false;
                //-----------------------------------

                [STAThread]
                static void Main()
                {
                        loginOK = false;
                        login = new FrmLogin();
                        Application.Run(login);
               
                        if (loginOK)
                        {
                                MDIMain m = new MDIMain();                               

                                login.Close();
                                Application.Run(m);
                        }
                }




C#,ASP.NET마스터 8일 56시간   09-05
C#,ASP.NET마스터 18일 54시간   09-06
C#,ASP.NET마스터 8일 56시간   09-07

댓글 없음:

댓글 쓰기