2012-03-28 32 views
2

目前我在做一个小项目,用户注册密码字段是在数据库加密,因此我需要使用MD5算法还可以对用户进行认证,但我的代码不能正常工作,每当我试图输入它输入的正确密码(未加密),但后来我发现我输入的任何密码,系统都会接受它,即使它不匹配数据库。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()); 

       } 

      } 

     } 

    } 

我的Web服务:

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"); 
    } 

请帮助我,我要纠正这种的方式,系统将接受加密的文本的正确相当于例如I型:西班牙= wdhs3x9029但我试图键入菲律宾,它也接受。

回答

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()。

+0

当我使用FormsAuthentication.HashPasswordForStoringInConfigFile方法声明此方法/类已弃用时,我收到来自VS 2012的警告消息 – 2016-03-14 18:20:10