0
我想在基於C#的WPF中使用MySQL實現logIn表單。貝婁是我的代碼。當我嘗試運行該程序似乎一切都很好,但是當我試圖填補信息,我在網上收到錯誤:使用基於C#的WPF在登錄表單中散列密碼
salt = cmd.ExecuteScalar() as string;
它說的是:在上線「字段列表」未知列「鹽」 。我試圖做的是在我的表格中創建列鹽。首先,我沒有輸入數據並得到「錯誤的細節」,然後我插入了與我的密碼相同的數據,並得到相同的錯誤:「錯誤的細節」。你有什麼想法可以解決我的問題嗎?
這裏是我的代碼,其中的敏感數據被替換:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Security.Cryptography;
using System.Security.Authentication;
using System.Security.Permissions;
using System.Security.AccessControl;
using System.Security.Policy;
using System.Security.Principal;
using System.Security.Util;
namespace ECBSRecruitmentAgencySoftware
{
public partial class LogIn : Form
{
public LogIn()
{
InitializeComponent();
}
static byte[] GenerateSaltedHash(string plainText, string salt)
{
HashAlgorithm algorithm = new SHA256Managed();
byte[] plainTextBytes = System.Text.Encoding.Unicode.GetBytes(plainText);
byte[] saltBytes = Convert.FromBase64String(salt);
byte[] plainTextWithSaltBytes = new byte[plainTextBytes.Length + saltBytes.Length];
saltBytes.CopyTo(plainTextWithSaltBytes, 0);
plainTextBytes.CopyTo(plainTextWithSaltBytes, salt.Length);
byte[] hash = algorithm.ComputeHash(plainTextWithSaltBytes);
return hash;
}
public bool tryLogin(string username , string password)
{
using (var con = new MySqlConnection("host=tara.rdb.superhosting.bg;user=sozopouk;password=27051996;database=sozopouk_test2;"))
{
con.Open();
var salt = string.Empty;
using (var cmd = new MySqlCommand("Select salt From niki where user_name = @username", con))
{
cmd.Parameters.AddWithValue("@username", username);
salt = cmd.ExecuteScalar() as string;
}
if (string.IsNullOrEmpty(salt)) return false;
var hashedPassword = GenerateSaltedHash(password, salt);
using (var cmd = new MySqlCommand("Select * FROM niki WHERE user_name = @username and user_password = @password", con))
{
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@password", hashedPassword);
using (var reader = cmd.ExecuteReader())
{
return reader.Read();
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
if (tryLogin(user.Text, pass.Text) == true)
{
MainScreen F2 = new MainScreen();
F2.Show();
this.Hide();
}
else MessageBox.Show("Wrong details!");
}
}
}
我不明白我不得不更換返回鹽的價值? –
對於用戶,鹽的值爲空/空,您傳入tryLogin()函數。如果您將在鹽田中爲該用戶在表格中輸入一些值。然後代碼將完美工作。 –
我在我的niki表的salt字段中插入了數據:12300,但仍然出現錯誤。任何想法? –