我在数据库中有一个密码字段经过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字符串正确的话,应该在页面是很容易比较滴。谢各位大虾给小弟帮个忙~~~~~~~

解决方案 »

  1.   

    懒得看了,随便Google下"ASP.net MD5" 都几十篇,楼主自己搞定吧.
      

  2.   

    其实我看不懂你在干什么,一般是把用户输入Hash(),从数据库取出已经Hash()过的字符串,然后比较两者是否一样.找了找以前的东西,没有找到数据库的,只找到这个保存在TXT的.差不多,自己看看.:#region 更改密码
    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 更改密码
      

  3.   

    这是登录页:
    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>");
    }
    }
      

  4.   

    以上是我说的第一种情况,直接从数据库取,和页面输入的值做比较,这个不成功
    ------------------------------
    说的题外话!你不觉得这样,select count(ID) from admin where User=用户名 and psw =加密过的密文如果返回是1就是允许登录。不更方便?
      

  5.   

    你页面的输入的值应该再次加密,比较两个密文是否一样.否则你用可解密的Sha
      

  6.   

    this.sqlInsertCommand1.Parameters["@Admin_pwd"].Value =str; ???