168 lines
6.8 KiB
C#
168 lines
6.8 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using MySql.Data.MySqlClient;
|
|
using Dapper;
|
|
|
|
namespace 全自动水压检测仪.DATA
|
|
{
|
|
/// <summary>
|
|
/// 登录调试辅助类
|
|
/// 用于测试和调试登录功能
|
|
/// </summary>
|
|
public static class LoginDebugHelper
|
|
{
|
|
private static readonly string _connectionString = "Server=localhost;Database=fullautowaterpressure;User=root;Password=123456;port=3306;charset=utf8;";
|
|
|
|
/// <summary>
|
|
/// 测试密码加密和验证
|
|
/// </summary>
|
|
public static void TestPasswordEncryption()
|
|
{
|
|
Debug.WriteLine("=== 测试密码加密 ===");
|
|
|
|
// 测试 admin123
|
|
string salt1 = PasswordHelper.GenerateSalt();
|
|
string hash1 = PasswordHelper.HashPassword("admin123", salt1);
|
|
bool verify1 = PasswordHelper.VerifyPassword("admin123", hash1, salt1);
|
|
|
|
Debug.WriteLine($"密码: admin123");
|
|
Debug.WriteLine($"盐值: {salt1}");
|
|
Debug.WriteLine($"哈希: {hash1}");
|
|
Debug.WriteLine($"验证: {verify1}");
|
|
Debug.WriteLine("");
|
|
|
|
// 测试 123
|
|
string salt2 = PasswordHelper.GenerateSalt();
|
|
string hash2 = PasswordHelper.HashPassword("123", salt2);
|
|
bool verify2 = PasswordHelper.VerifyPassword("123", hash2, salt2);
|
|
|
|
Debug.WriteLine($"密码: 123");
|
|
Debug.WriteLine($"盐值: {salt2}");
|
|
Debug.WriteLine($"哈希: {hash2}");
|
|
Debug.WriteLine($"验证: {verify2}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查看数据库中的用户信息
|
|
/// </summary>
|
|
public static void ShowDatabaseUsers()
|
|
{
|
|
try
|
|
{
|
|
using (var connection = new MySqlConnection(_connectionString))
|
|
{
|
|
connection.Open();
|
|
|
|
var users = connection.Query<User>("SELECT * FROM sys_users");
|
|
|
|
Debug.WriteLine("=== 数据库用户列表 ===");
|
|
foreach (var user in users)
|
|
{
|
|
Debug.WriteLine($"ID: {user.Id}");
|
|
Debug.WriteLine($"用户名: {user.Username}");
|
|
Debug.WriteLine($"角色: {(user.UserRole == 1 ? "管理员" : "普通用户")}");
|
|
Debug.WriteLine($"状态: {(user.Status == 1 ? "启用" : "禁用")}");
|
|
Debug.WriteLine($"密码哈希: {user.PasswordHash}");
|
|
Debug.WriteLine($"盐值: {user.Salt}");
|
|
Debug.WriteLine($"创建时间: {user.CreateTime}");
|
|
Debug.WriteLine("---");
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine($"查询用户失败: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 重置所有默认用户
|
|
/// </summary>
|
|
public static void ResetDefaultUsers()
|
|
{
|
|
try
|
|
{
|
|
using (var connection = new MySqlConnection(_connectionString))
|
|
{
|
|
connection.Open();
|
|
|
|
// 删除现有用户
|
|
connection.Execute("DELETE FROM sys_users WHERE username IN ('admin', 'csi')");
|
|
Debug.WriteLine("已删除现有默认用户");
|
|
|
|
// 重新创建 admin
|
|
string salt1 = PasswordHelper.GenerateSalt();
|
|
string hash1 = PasswordHelper.HashPassword("admin123", salt1);
|
|
connection.Execute(@"
|
|
INSERT INTO sys_users (username, password_hash, salt, user_role, status, create_time)
|
|
VALUES (@username, @hash, @salt, @role, 1, NOW())",
|
|
new { username = "admin", hash = hash1, salt = salt1, role = 1 });
|
|
Debug.WriteLine("✓ 创建管理员: admin / admin123");
|
|
|
|
// 重新创建 cgx
|
|
string salt2 = PasswordHelper.GenerateSalt();
|
|
string hash2 = PasswordHelper.HashPassword("123456", salt2);
|
|
connection.Execute(@"
|
|
INSERT INTO sys_users (username, password_hash, salt, user_role, status, create_time)
|
|
VALUES (@username, @hash, @salt, @role, 1, NOW())",
|
|
new { username = "csi", hash = hash2, salt = salt2, role = 0 });
|
|
Debug.WriteLine("✓ 创建普通用户: csi / 123456");
|
|
|
|
Debug.WriteLine("默认用户重置完成!");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine($"重置用户失败: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试登录
|
|
/// </summary>
|
|
public static void TestLogin(string username, string password)
|
|
{
|
|
try
|
|
{
|
|
Debug.WriteLine($"=== 测试登录: {username} ===");
|
|
|
|
var repository = new UserRepository();
|
|
var user = repository.GetUserByUsername(username);
|
|
|
|
if (user == null)
|
|
{
|
|
Debug.WriteLine("❌ 用户不存在");
|
|
return;
|
|
}
|
|
|
|
Debug.WriteLine($"✓ 找到用户: {user.Username}");
|
|
Debug.WriteLine($" 角色: {(user.UserRole == 1 ? "管理员" : "普通用户")}");
|
|
Debug.WriteLine($" 状态: {(user.Status == 1 ? "启用" : "禁用")}");
|
|
Debug.WriteLine($" 密码哈希: {user.PasswordHash}");
|
|
Debug.WriteLine($" 盐值: {user.Salt}");
|
|
|
|
bool isValid = PasswordHelper.VerifyPassword(password, user.PasswordHash, user.Salt);
|
|
|
|
if (isValid)
|
|
{
|
|
Debug.WriteLine("✓ 密码验证成功!");
|
|
}
|
|
else
|
|
{
|
|
Debug.WriteLine("❌ 密码验证失败!");
|
|
|
|
// 测试重新生成哈希
|
|
string testHash = PasswordHelper.HashPassword(password, user.Salt);
|
|
Debug.WriteLine($" 输入密码的哈希: {testHash}");
|
|
Debug.WriteLine($" 数据库的哈希: {user.PasswordHash}");
|
|
Debug.WriteLine($" 是否匹配: {testHash == user.PasswordHash}");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine($"测试登录失败: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|