ASP.NET使用md5对用户进行身份验证

时间:2012-03-28 07:00:32

标签: asp.net

目前我正在做一个小项目,用户注册密码字段在数据库中加密,因此我需要使用md5算法验证用户,但我的代码无法正常工作,每当我尝试输入正确的密码(未加密)它进入但后来我发现我键入的任何密码,系统将接受它,即使它在数据库中不匹配。

你能帮帮我吗?这是我的代码:

 protected void btnSubmit_Click(object sender, EventArgs e)
    {

        string pAssword = txtPassword.Text;
        MD5CryptoServiceProvider encryptor = new MD5CryptoServiceProvider();
        byte[] encryptedValue;
        UTF8Encoding encoder = new UTF8Encoding();
        encryptedValue = encryptor.ComputeHash(encoder.GetBytes(pAssword));

        DataSet ds = new DataSet();
        ds = (startWebService.getAllUsers());
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dRow in ds.Tables[0].Rows)
            {

                string userName = dRow["UserName"].ToString();
                string passWord = dRow["Password"].ToString();
                string acctNo = dRow["AccountNumber"].ToString();

                if (userName == txtUsername.Text.ToString() && acctNo == txtAcctNo.Text.ToString() && passWord == encryptedValue.ToString() )
                {
                    FormsAuthentication.RedirectFromLoginPage(txtUsername.Text.ToString(), false);
                    lblError.Text = "You got it!";
                    Response.Redirect("MyAccount.aspx");
                }
                else
                {
                    this.lblError.ForeColor = System.Drawing.Color.Red;
                    this.lblError.Text = "Either you have been type an incorrect network credentials or you have reached the maximum login attempts for your account.Please try again or contact the system administrator.";

                    startWebService.updateFailedLogin(txtAcctNo.Text.ToString(), txtUsername.Text.ToString());

                }

            }

        }

    }

我的网络服务:

    private DataSet GetDataSet(string strSPROC)
    {

        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = strSPROC;
        conn.Open();
        SqlDataAdapter myDataAdapter = new SqlDataAdapter();
        myDataAdapter.SelectCommand = cmd;
        DataSet dsMT = new DataSet();
        myDataAdapter.Fill(dsMT);
        return dsMT;
        conn.Close();
    }
  [WebMethod]
    public DataSet getAllUsers()
    {
        return GetDataSet("ELMS_ALLINTERNETUSERS");
    }

请帮助我,我必须以一种系统将接受正确等效的加密文本的方式纠正这个问题,例如我输入:spain = wdhs3x9029但我试图输入菲律宾,它也接受。

1 个答案:

答案 0 :(得分:3)

有一个很好的内置哈希密码方法(可以使用MD5):

string encryptedValue = FormsAuthentication.HashPasswordForStoringInConfigFile(pAssword, "MD5");

您可以阅读有关此方法的更多信息here。如果你需要重新发明轮子,那么我建议你把哈希作为字符串的方法更改为更像这样的东西:

MD5CryptoServiceProvider encryptor = new MD5CryptoServiceProvider();
UTF8Encoding encoder = new UTF8Encoding();

byte[] encryptedValueBytes = encryptor.ComputeHash(encoder.GetBytes(pAssword));
StringBuilder encryptedValueBuilder = new StringBuilder();
for (int i = 0; i < encryptedValueBytes.Length; i++)
{
    encryptedValueBuilder.Append(data[i].ToString("x2"));
}
string encryptedValue = encryptedValueBuilder.ToString();

而不是字节数组上的简单.ToString()。