test gelöscht

This commit is contained in:
Yongkang Zhang 2025-03-19 09:44:27 +01:00
parent fdf61b30df
commit d740e9a298
3 changed files with 8 additions and 44 deletions

View File

@ -25,7 +25,7 @@
<PasswordBox Width="200" Height="30" Margin="0,5,0,0" x:Name="PasswordTextBox" />
<!-- 登录按钮 -->
<Button Height="30" Width="100" Content="Log in" Margin="0,20,0,0" Click="loginButton_Click" />
<Button Height="30" Width="100" Content="Log in" Margin="0,20,0,0" Click="LoginButton_Click" />
</StackPanel>
</Border>
</Grid>

View File

@ -1,4 +1,6 @@
using System.Data;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;
using System.Windows;
using System.Windows.Controls;
@ -23,51 +25,11 @@ public partial class MainWindow : MetroWindow
public MainWindow()
{
InitializeComponent();
// TestConnection();
// Console.ReadKey();
//TestConnection();
//Console.ReadKey();
}
private void loginButton_Click(object sender, RoutedEventArgs e)
{
// 从用户输入中获取用户名和密码
string username = UsernameTextBox.Text; // 获取用户名
string password = PasswordTextBox.Password; // 获取密码
// 调用 ValidateUser 方法验证用户凭据
if (ValidateUser(username, password))
{
MessageBox.Show("ok", "Login Successful", MessageBoxButton.OK, MessageBoxImage.Error);
}
else
{
// 如果验证失败,显示错误信息
MessageBox.Show("Invalid username or password.", "Login Failed", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private bool ValidateUser(string username, string password)
{
// 获取数据库连接
using (var con = GetConnection())
{
con.Open(); // 打开数据库连接
string query = "SELECT COUNT(*) FROM USER WHERE UserName = @username AND Password = @password;";
using (var cmd = new NpgsqlCommand(query, con))
{
// 使用参数化查询防止 SQL 注入
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@password", password);
// 执行查询并获取结果
int count = (int)cmd.ExecuteScalar();
return count > 0; // 如果匹配记录数量大于 0返回 true
}
}
}
private static void TestConnection()
{
using (NpgsqlConnection con = GetConnection())
@ -81,7 +43,7 @@ public partial class MainWindow : MetroWindow
}
private static NpgsqlConnection GetConnection()
{
return new NpgsqlConnection(@"Server=localhost;Port=5432;User Id=postgres;Password=postgres;Database=postgres;");
return new NpgsqlConnection(@"Server=localhost;Port=7854;User Id=postgres;Database=postgres;");
}
//class Program

View File

@ -24,7 +24,9 @@
<ItemGroup>
<PackageReference Include="MahApps.Metro" Version="2.4.10" />
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Negotiate" Version="9.0.3" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.69.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Npgsql" Version="9.0.3" />
<PackageReference Include="PdfPig" Version="0.1.10" />