编程与数学 03-008 《看潮企业管理软件》项目开发 05 入口验证 3-1
摘要:本文围绕.NET Framework 4.72 C# WinForms"看潮ERP"登录体系,给出"先登录后主窗"的标准入口实现:Program.cs中用ShowDialog唤起登录窗,返回OK才Run主窗;登录窗以DevExpress控件构建,支持多账套、多配置文件、密码掩码、失败锁定、加密存储及皮肤切换;另提供可视化编辑窗维护kcxtzt*.xml,实现账套CRUD、颜色图标等个性化设置;完整代码与流程示例可直接集成到企业项目,兼顾安全、易用与可扩展。
关键词:C# WinForms、登录验证、DevExpress、多账套、XML配置、密码加密、企业ERP、入口流程、皮肤切换、CRUD管理
人工智能助手:DeepSeek、Kimi
一、常规设计
在.NET Framework 4.72 C#程序中实现先显示登录窗口再打开主窗口的流程,可以通过以下方式设计:
(一)修改Program.cs入口点(推荐方案)
csharp
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// 先显示登录窗口
using (var loginForm = new LoginForm())
{
if (loginForm.ShowDialog() == DialogResult.OK)
{
// 登录成功,显示主窗口
Application.Run(new MainForm());
}
else
{
// 登录失败,退出程序
Application.Exit();
}
}
}
}
(二)登录窗口(LoginForm)设计要点
csharp
public partial class LoginForm : DevExpress.XtraEditors.XtraForm
{
public LoginForm()
{
InitializeComponent();
// 设置密码框属性
txtPassword.Properties.PasswordChar = '*';
txtPassword.Properties.UseSystemPasswordChar = true;
}
private void btnLogin_Click(object sender, EventArgs e)
{
if (ValidateLogin(txtUsername.Text, txtPassword.Text))
{
// 设置DialogResult为OK并关闭窗口
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
XtraMessageBox.Show("用户名或密码错误", "登录失败",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private bool ValidateLogin(string username, string password)
{
// 这里实现实际的验证逻辑,例如:
// - 数据库验证
// - Active Directory验证
// - 配置文件验证
// 示例:简单验证
return username == "admin" && password == "123456";
}
}
(三)主窗口(MainForm)设计要点
csharp
public partial class MainForm : DevExpress.XtraEditors.XtraForm
{
public MainForm()
{
InitializeComponent();
this.Text = $"主窗口 - 当前用户: {CurrentUser.Username}";
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
// 确认退出
if (XtraMessageBox.Show("确定要退出系统吗?", "确认",
MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
base.OnFormClosing(e);
}
}
(四)用户信息管理(静态类)
csharp
public static class CurrentUser
{
public static string Username { get; set; }
public static string Role { get; set; }
public static DateTime LoginTime { get; set; }
public static void Clear()
{
Username = string.Empty;
Role = string.Empty;
}
}
(五)增强安全性设计
- 密码加密处理:
csharp
using System.Security.Cryptography;
public static class SecurityHelper
{
public static string EncryptPassword(string password)
{
using (SHA256 sha256 = SHA256.Create())
{
byte[] bytes = Encoding.UTF8.GetBytes(password);
byte[] hash = sha256.ComputeHash(bytes);
return Convert.ToBase64String(hash);
}
}
}
- 登录失败锁定:
csharp
private int failedAttempts = 0;
private DateTime? lastFailedAttempt;
private void btnLogin_Click(object sender, EventArgs e)
{
if (lastFailedAttempt.HasValue &&
(DateTime.Now - lastFailedAttempt.Value).TotalMinutes < 5 &&
failedAttempts >= 3)
{
XtraMessageBox.Show("登录失败次数过多,请5分钟后再试");
return;
}
if (ValidateLogin(txtUsername.Text, txtPassword.Text))
{
// 登录成功处理...
}
else
{
failedAttempts++;
lastFailedAttempt = DateTime.Now;
// 显示错误...
}
}
(六)完整流程说明
- 程序启动 → 显示登录窗口
- 用户输入凭据 → 点击登录
- 验证成功:
- 设置
DialogResult.OK - 关闭登录窗口
- Program.cs继续执行,打开主窗口
- 设置
- 验证失败:
- 显示错误消息
- 保持登录窗口打开
- 主窗口关闭时 → 整个应用程序退出
(七)使用DevExpress控件时的注意事项
- 登录窗口使用
XtraForm:
csharp
public partial class LoginForm : DevExpress.XtraEditors.XtraForm
- 消息框使用
XtraMessageBox:
csharp
XtraMessageBox.Show("消息内容", "标题", MessageBoxButtons.OK, MessageBoxIcon.Information);
- 密码框增强设置:
csharp
// 在LoginForm的构造函数或设计器中
txtPassword.Properties.PasswordChar = '●';
txtPassword.Properties.UseSystemPasswordChar = true;
txtPassword.Properties.MaxLength = 20;
这种设计模式符合Windows应用程序的标准安全实践,提供了清晰的用户验证流程,并且易于扩展添加更多安全功能。
以下为本项目的实际设计。
二、入口程序调用
在Program.cs中调用登录窗口。
C#
// 创建登录窗体实例,使用using确保窗体资源会被正确释放
using (var loginForm = new FmLogin())
{
// 以模态对话框形式显示登录窗体,程序将在此等待登录操作完成
loginForm.ShowDialog();
// 根据KcDb类的SFlj属性判断是否成功连接数据库(或登录成功)
if (KcDb.SFlj)
{
// 如果连接成功,启动主消息循环并运行主窗体(FmMain),程序进入主界面
Application.Run(new FmMain());
}
else
{
// 如果连接失败,退出应用程序
Application.Exit();
}
} // 登录窗体在此处自动释放