2012-03-28 58 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