我在数据库中有一个密码字段经过MD5加密过了,但是在页面怎么也取不到,和txtbox的值没法做比较,也就实现不了登录。(这个问题是我先在数据库自己添加的MD5加密后的字符串,但取出值没法比较)
但是我用另种方法从页面把密码加密到数据库中后,再在页面做比较就可以。
请问大虾们这是怎么回事??
我觉得就是在取值时候出现的问题我把两段代码附上,有好心人能帮一下这个初学者的小弟不 >_<// private void Button1_Click(object sender, System.EventArgs e)
// {
//
// this.sqlConnection1 = new System.Data.SqlClient.SqlConnection(con);
// this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand("",sqlConnection1);
// string str = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.TextBox1.Text,"MD5");
//
// this.sqlSelectCommand1.CommandText ="select Admin_pwd from AdminLogin where Admin_pwd ='"+str+"'";
// this.sqlConnection1.Open();
// this.Response.Write(this.sqlConnection1.State.ToString());
// this.Label1.Text = str;
// this.Response.Write(this.sqlSelectCommand1.CommandText.ToString());
// if(this.sqlSelectCommand1.ExecuteScalar()!=null || this.TextBox1.Text!="")
// {
// this.Response.Write("ok..............");
// }
// else
// {
// this.Response.Write("sorry.........");
// }
// this.sqlConnection1.Close();
// }
// }
//}以上是我说的第一种情况,直接从数据库取,和页面输入的值做比较,这个不成功 private void Button1_Click(object sender, System.EventArgs e)
{
this.con.Open();
string str = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.TextBox1.Text,"MD5"); this.sqlInsertCommand1.Parameters["@Admin_pwd"].Value =str;
this.sqlInsertCommand1.ExecuteNonQuery();
this.con.Close();
} private void Button2_Click(object sender, System.EventArgs e)
{
this.con.Open();
string str = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.TextBox2.Text,"MD5"); this.sqlSelectCommand1.CommandText ="select Admin_pwd from AdminLogin where Admin_pwd = '"+str+"'";
if(this.sqlSelectCommand1.ExecuteScalar()!=null)
{
this.Response.Write("ok..............");
}
else
{
this.Response.Write("sorry.........");
}
this.con.Close(); }
这段代码是在页面经加密存到数据库后,然后在页面在取值和输入的值做比较,这个可以成功所以我就想了,如果是初始管理员的话,总不可能还要在页面往数据库添加吧。应该是在数据库中填的MD5字符串正确的话,应该在页面是很容易比较滴。谢各位大虾给小弟帮个忙~~~~~~~
但是我用另种方法从页面把密码加密到数据库中后,再在页面做比较就可以。
请问大虾们这是怎么回事??
我觉得就是在取值时候出现的问题我把两段代码附上,有好心人能帮一下这个初学者的小弟不 >_<// private void Button1_Click(object sender, System.EventArgs e)
// {
//
// this.sqlConnection1 = new System.Data.SqlClient.SqlConnection(con);
// this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand("",sqlConnection1);
// string str = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.TextBox1.Text,"MD5");
//
// this.sqlSelectCommand1.CommandText ="select Admin_pwd from AdminLogin where Admin_pwd ='"+str+"'";
// this.sqlConnection1.Open();
// this.Response.Write(this.sqlConnection1.State.ToString());
// this.Label1.Text = str;
// this.Response.Write(this.sqlSelectCommand1.CommandText.ToString());
// if(this.sqlSelectCommand1.ExecuteScalar()!=null || this.TextBox1.Text!="")
// {
// this.Response.Write("ok..............");
// }
// else
// {
// this.Response.Write("sorry.........");
// }
// this.sqlConnection1.Close();
// }
// }
//}以上是我说的第一种情况,直接从数据库取,和页面输入的值做比较,这个不成功 private void Button1_Click(object sender, System.EventArgs e)
{
this.con.Open();
string str = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.TextBox1.Text,"MD5"); this.sqlInsertCommand1.Parameters["@Admin_pwd"].Value =str;
this.sqlInsertCommand1.ExecuteNonQuery();
this.con.Close();
} private void Button2_Click(object sender, System.EventArgs e)
{
this.con.Open();
string str = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.TextBox2.Text,"MD5"); this.sqlSelectCommand1.CommandText ="select Admin_pwd from AdminLogin where Admin_pwd = '"+str+"'";
if(this.sqlSelectCommand1.ExecuteScalar()!=null)
{
this.Response.Write("ok..............");
}
else
{
this.Response.Write("sorry.........");
}
this.con.Close(); }
这段代码是在页面经加密存到数据库后,然后在页面在取值和输入的值做比较,这个可以成功所以我就想了,如果是初始管理员的话,总不可能还要在页面往数据库添加吧。应该是在数据库中填的MD5字符串正确的话,应该在页面是很容易比较滴。谢各位大虾给小弟帮个忙~~~~~~~
解决方案 »
- 求一正则
- 问个单点登陆的问题
- 网站变灰代码分享(高手误喷)
- 顶着有分!!关于异步的问题-------几个带参数的方法要求用异步来提交速度
- 上传文件是,在对话框中怎么去掉不要的文件类型(FileUpload控件)
- .Net2005中如何将一个DataSet拖动到页面上?
- 救命啊!! 我公司有个内部网站,希望只有内部的人才能访问,该怎样限制呢?
- 谁知道淘宝网上注册新用户时点击输入框时,右边的提示框怎么做啊
- 如何从datagrid里查找一个控件。
- 那里有好是教程下,电子版的更好
- 新闻显示页面的新闻内容有javascript错误令到下面的评论提交不了怎么解决啊?急啊,帮个忙吧
- 初学者~~~拜托大家了!!!
private void btn_Menu_ChangePassword_Click(object sender, System.EventArgs e)
{
panel_change_password.Visible =true;
btn_Menu_ChangePassword.Visible =false;
}private void btn_ChangePassword_Click(object sender, System.EventArgs e)
{
panel_change_password.Visible =false;
btn_Menu_ChangePassword.Visible =true;
string strHash=System.Web .Security .FormsAuthentication .HashPasswordForStoringInConfigFile (txtPassword.Text ,"MD5");
//把Password hash 写入key.txt
try
{
string strPath=Server.MapPath ("..").ToString ();
FileInfo fi=new FileInfo(strPath+"\\key.txt");
System.IO .StreamWriter sw=fi.CreateText();
sw.WriteLine (strHash);
sw.Close ();
fi.Refresh();
}
catch(Exception ex)
{
WriteError.WriteException (ex,"Error.txt");
}
//finally { }
//提示更改完成.
Response.Write ("<script>alert('更改成功')</script>"); }
#endregion 更改密码
private void btnLogin_Click(object sender, System.EventArgs e)
{
string passInFile;
string pass=System.Web .Security .FormsAuthentication .HashPasswordForStoringInConfigFile (txtLogin.Text.Trim (),"MD5");
System.IO.StreamReader sr;
try
{
FileInfo fi=new FileInfo (Path.Combine (MapPath(".."),"key.txt"));
sr=fi.OpenText ();
passInFile=sr.ReadLine ().Trim ();
sr.Close ();
}
catch(Exception ex){throw ex;}
finally{
if(!CheckLiscen.Checkliscen ())Response.Redirect ("images/lc.htm");
}
if(passInFile==pass)
{
System.Web .Security .FormsAuthentication .Authenticate ("admin","DontChange");
System.Web .Security .FormsAuthentication .SetAuthCookie("admin",false);
Response.Redirect ("Admin/Administrator.aspx");
}
else
{
Response.Write ("<script>alert('密码不正确');</script>");
}
}
------------------------------
说的题外话!你不觉得这样,select count(ID) from admin where User=用户名 and psw =加密过的密文如果返回是1就是允许登录。不更方便?