c# - 密码哈希算法及程序实际运行

标签 c# mysql

我是 C# 编程新手,尝试使用 C# 和 MySQL 数据库在 WPF 中创建登录表单。当我运行 WPF 并尝试插入数据时,我在这一行收到错误:

using (var cmd = new MySqlCommand("Select salt From niki where user_name = @username"))
{
    cmd.Parameters.AddWithValue("@username", username);
    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"))
                 {
                     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"))
                 {
                    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!");

        }        
        }


}

你的意思是我必须更新:

 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"))
                     {
                         cmd.Parameters.AddWithValue("@username", username);

                         salt = cmd.ExecuteScalar() as string;
                     }
enter code here
enter code here

使用您生成的代码?如果是,我实际上如何连接到我的 MySQL 数据库?

最佳答案

好吧,在尝试读取或写入数据库之前,您应该打开一个连接,然后发出命令。

using (MySqlConnection cn = GetConnection())
{
    cn.Open();
    // create the command and link it to the connection
    using (var cmd = new MySqlCommand("Select salt From niki where user_name = @username", cn)) 
    { 
        cmd.Parameters.AddWithValue("@username", username); 
        salt = cmd.ExecuteScalar() as string; 
    }
} 

public MySqlConnection GetConnection()
{
     MySqlConnection cn = new MySqlConnection("Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;");
     return cn;
}

编辑:拼图中缺失的部分在这里:

MySqlCommand cmd = MySqlCommand("your_query_text", cn)

关于c# - 密码哈希算法及程序实际运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10628138/

相关文章:

c# - using 语句中的成员范围

c# - silverlight 中的动态图像源绑定(bind)

c# - FileSystemWatcher 问题

c# - "new"关键字前引用类型的值

c# - 将电子邮件作为附件附加到另一封电子邮件

sql - 在 SQL 中,如果可用,如何获取具有特定列值的行,否则获取任何其他行

PHP PDO 不会在重复键插入时抛出异常

mysql - SQL查询和计数

php - 无法在 XAMPP 中打开现有页面?

mysql - 在mysql中使用select插入后创建触发器