제목 : 7.3.1. 회원 로그인/로그아웃 처리(세션인증 사용) : Login.ascx.cs
namespace Home.Member
{
using System;
using System.Configuration;//
using System.Data.SqlClient;//
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public class Login1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Panel Panel1;
protected System.Web.UI.WebControls.Panel Panel2;
protected System.Web.UI.WebControls.Label lblUserName;
protected System.Web.UI.WebControls.TextBox txtUserID;
protected System.Web.UI.WebControls.TextBox txtPassword;
protected System.Web.UI.WebControls.LinkButton btnLogin;
protected System.Web.UI.WebControls.LinkButton btnRegister;
protected System.Web.UI.WebControls.LinkButton btnLogout;
protected System.Web.UI.WebControls.Label lblUserID;
// 초기화
private void Page_Load(object sender, System.EventArgs e)
{
if(Session["UserID"] == null
|| Session["UserID"].ToString() == "")//로그인 하지 않음
{
this.Panel1.Visible = true;
this.Panel2.Visible = false;
}
else
{
this.Panel1.Visible = false;
this.Panel2.Visible = true;
this.lblUserName.Text = Session["UserName"].ToString();
this.lblUserID.Text = Session["UserID"].ToString();
}
}
// 로그인
private void btnLogin_Click(object sender, System.EventArgs e)
{
string strUserID = this.txtUserID.Text;//아이디
string strPassword = this.txtPassword.Text;//비밀번호
SqlConnection objCon = new SqlConnection(
ConfigurationSettings.AppSettings["ConnectionString"]);
objCon.Open();
SqlCommand objCmd = new SqlCommand();
objCmd.Connection = objCon;
objCmd.CommandText =
"Select * From Users Where UserID = '"
+ strUserID + "' And Password = '" + strPassword + "'";
objCmd.CommandType = CommandType.Text;
SqlDataReader objDr = objCmd.ExecuteReader();
if(objDr.Read()){
Session["UserID"] = objDr["UserID"];
Session["UserName"] = objDr["UserName"];
this.Panel1.Visible = false;
this.Panel2.Visible = true;
this.lblUserName.Text = Session["UserName"].ToString();
this.lblUserID.Text = Session["UserID"].ToString();
}
else{
string strMsg =
"<script>alert(\"아이디 또는 암호가 틀립니다.\");</script>";
Page.RegisterClientScriptBlock("no", strMsg);
}
objCon.Close();
}
// 회원 가입
private void btnRegister_Click(object sender, System.EventArgs e)
{
Response.Redirect("/Home/Member/Register.aspx");
}
// 로그아웃
private void btnLogout_Click(object sender, System.EventArgs e)
{
Session.Abandon();//세션 날리기
this.Panel1.Visible = true;
this.Panel2.Visible = false;
}
#region Web Form 디자이너에서 생성한 코드
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 이 호출은 ASP.NET Web Form 디자이너에 필요합니다.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 디자이너 지원에 필요한 메서드입니다. 이 메서드의 내용을
/// 코드 편집기로 수정하지 마십시오.
/// </summary>
private void InitializeComponent()
{
this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
this.btnRegister.Click += new System.EventHandler(this.btnRegister_Click);
this.btnLogout.Click += new System.EventHandler(this.btnLogout_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}