2009년 08월 26일
[SQL] 사용자 비밀번호를 암호화하여 저장하고 비교하기
신규 등록하는 회원의 사용자 비밀번호를 char 또는 varchar로 그대로 저장하면,
후에 select 문을 사용하여 저장된 비밀번호를 조회할 수 있다.
insert 문으로 저장시에 아래의 pwdencrypt() SQL 내장함수를 쓰면 사용자 비밀번호를 암호화하여 저장한다.
대신, 나중에 로그인 처리를 할 때에 이렇게 암호화된 비밀번호를 조회할 수 없기 때문에, SQL,에서는 pwdcompare() 함수를 함께 제공한다.
0. ASP.NET web.config 파일에 connectionString 저장
========================================
<?xml version="1.0"?>
<configuration>
<appSettings>
</appSettings>
<!-- ConfigurationSettings.AppSettings["con_str"] -->
<connectionStrings>
<add name="con_str" connectionString="server=mycom; database=mydb; user id = sa; password=000" providerName="System.Data.SqlClient"/>
<!-- System.Web.Configuration.WebConfigurationManager.ConnectionStrings["con_str"].ConnectionString -->
</connectionStrings>
========================================
1. 사용자 아이디와 비밀번호를 입력하기
=======================================
string m_id = textUserid.Text.ToLower(); // id를 모두 소문자로 변환
string m_pwd = textPasswd.Text; // 비밀번호
string m_name = textName.Text; // 사용자 이름
SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["con_str"].ConnectionString); // web.config에 설정한 DB 연결문
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "INSERT INTO users (userid, passwd, name1) VALUES (@id,convert(varbinary(255),pwdencrypt(@pwd)))";
cmd.Parameters.Add("@id", SqlDbType.VarChar, 20);
cmd.Parameters.Add("@pwd", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@name", SqlDbType.VarChar, 20);
cmd.Parameters["@id"].Value = m_id;
cmd.Parameters["@pwd"].Value = m_pwd;
cmd.Parameters["@name"].Value = m_name;
try
{
con.Open();
if (cmd.ExecuteNonQuery() > 0)
labelMsg1.Text = "New account is created : " + m_id + "(" + m_name + ")";
}
catch (SqlException ex)
{
labelMsg1.Text = "SQL exception error (" + ex.Number + "): " + ex.Message;
}
catch (InvalidOperationException ex)
{
labelMsg1.Text = "Invalid operation error : " + ex.Message;
}
catch (Exception ex)
{
labelMsg1.Text = "Exception error : " + ex.Message;
}
finally
{
con.Close();
}
========================================
2. 사용자 로그인시에 아이디와 비밀번호 확인하기
=======================================
private bool Authenticate(string id, string pwd)
{
bool res = false;
SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["con_str"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select pwdcompare(@pwd,passwd), name1, userlevel 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();
reader.Read();
if (Convert.ToInt32(reader[0])>0)
{
Session["name"] = reader[1];
Session["userlevel"] = reader[2];
Session["user_id"] = id;
res = true;
}
else
labelMsg2.Text = "Invalid User Id or Password";
}
catch (Exception ex)
{
labelMsg2.Text = ex.Message;
}
finally
{
if (reader != null) reader.Close();
con.Close();
}
return res;
}
이 글과 관련있는 글을 자동검색한 결과입니다 [?]
- 이 버전의 SQL Server에서는 사용자 인스턴스 로그인 플래그가 지원되지 않습니다. 연결이 닫힙니다. by 해피포터
- 로그인시 사용자 비번을 암호화 시킨후 쿠키생성 by CHOCOLATE
- 티스토리 Blog API 지원.. 이거 너무한데.. by 권남
- ...나보고 어쩌란거야... by 아이온
- 뭐지? by 미자
# by | 2009/08/26 23:32 | 프로그래밍 | 트랙백 | 덧글(0)





☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]