C# 登录功能(优雅且详细) 您所在的位置:网站首页 登录twitter代码 C# 登录功能(优雅且详细)

C# 登录功能(优雅且详细)

2024-05-25 23:24| 来源: 网络整理| 查看: 265

文章目录 前言一、需求描述二、功能设计1.登录界面设计2.数据库3.登录4.记住账号5.验证码 三、程序实现1.创建一个SqlHelper的工具类,用于查询数据2.验证登录信息功能的函数3.实现验证码功能写一个用于创建验证码的工具类更新验证码的方法验证验证码是否正确 4.保存账号及密码功能的实现在APP.config中先创建键值对用于储存复选框状态以及记录账号信息记录账号信息的函数在窗体加载的同时写入储存的登录信息 5.登录功能进入系统功能点击按钮 四、登录页面完整代码:五、运行效果后记

前言

此系列文章是有关贵州省第一届商务软件解决方案技能大赛试题的求解,下附试题以及项目源码。

试题

项目源码

一、需求描述 制作一个登陆界面,包含验证码,记住密码以及根据登录用户类型(RoleID)加载不同界面的功能。用户使用账号与密码登录系统。本系统的用户包括 3 种类型若用户的登录失败次数达到 3 次,那么系统应显示并且同时包含字母与数字的4位验证码,验证码字符输入正确方可登录。若选择记住密码,下次登录时直接显示上次输入的账号和密码 二、功能设计 1.登录界面设计

登录界面

2.数据库

获取登录id和password需要连接SQL server数据库,则需要创建一个用于链接数据库的类,在登录界面代码中调用。

SQLserver数据表结构: 数据表结构

3.登录 验证登录账号和密码获取登录账号的类型(以数字代表类型,一共有三种类型,分别是1,2,3)若验证错误次数超过三次,则生成验证码 4.记住账号

将账号信息储存到appconfig文件中

5.验证码 达到条件时创建验证码生成验证码图片验证码输入不正确或点击更新验证码 三、程序实现 1.创建一个SqlHelper的工具类,用于查询数据

有关数据库连接详细解析见下附链接 .Net连接SQL Server数据库的优雅方式(巨详细)

namespace OlympicManagementSystem.Util { public class SqlHelper { public static string ConStr { get; set; }//数据库连接字符串 public static DataTable ExecuteTable(string cmdStr)//获取数据表的方法,返回一个DataTable { using (SqlConnection con = new SqlConnection(ConStr)) { con.Open(); SqlCommand cmd = new SqlCommand(cmdStr, con); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adapter.Fill(ds); return ds.Tables[0]; } } public static int ExecuteNonQuery(string cmdStr)//修改数据表的方法,返回受影响行数 { using (SqlConnection con = new SqlConnection(ConStr)) { con.Open(); SqlCommand cmd = new SqlCommand(cmdStr, con); int rows = cmd.ExecuteNonQuery(); if (rows if (txbID.Text == "" || txbPwd.Text == "") { MessageBox.Show("用户名和密码不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } else {//Query user information string sql = "select * from Staff where LoginID = '" + txbID.Text + "' and Password = '" + txbPwd.Text + "'"; DataTable dataTable = SqlHelper.ExecuteTable(sql); if (dataTable.Rows.Count > 0) { DataRow dataRow = dataTable.Rows[0]; roleID = (int)dataRow["RoleID"]; Configuration cf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); cf.AppSettings.Settings["roleID"].Value = roleID.ToString();//record roleID cf.Save(); return true; } else { MessageBox.Show("用户名或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } } 3.实现验证码功能 写一个用于创建验证码的工具类

先创建验证码字符串,然后使用字符串生成图片

namespace OlympicManagementSystem.Util { class VerificationCode { ///生成随机验证字符串 public static string CreateRandomCode(int CodeLength) { int rand; char code; string randomCode = String.Empty;//随机验证码 //生成特定长度的验证码 Random random = new Random(); for (int i = 0; i code = (char)('A' + (char)(rand % 26));//随机取字母 } else if (rand % 3 == 2) { code = (char)('a' + (char)(rand % 26)); } else { code = (char)('0' + (char)(rand % 10)); } randomCode += code.ToString(); } return randomCode; } ///创建图片 public static void CreateImage(string strValidCode, PictureBox pbox) { try { int RandAnlge = 45;//旋转角度 int MapWidth = (int)(strValidCode.Length * 21); Bitmap map = new Bitmap(MapWidth, 28);//创建图片背景 Graphics graph = Graphics.FromImage(map); graph.Clear(Color.AliceBlue);//清除话画面,填充背景颜色 graph.DrawRectangle(new Pen(Color.Black, 0), 0, 0, map.Width - 1, map.Height - 1);//画一个边框 graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//模式 Random rand = new Random(); //背景噪点生成 Pen blackPen = new Pen(Color.LightGray, 0); for (int i = 0; i Color.Black, Color.Red, Color.DarkBlue, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple, Color.Green }; //定义字体 string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" }; for (int i = 0; i MessageBox.Show("验证图片创建错误"); } } } } 更新验证码的方法

验证码验证失败及点击验证码需要更新,创建一个更新验证码的方法

private void UpdateVerificationCode()//Creat / update verification { verificationCode = VerificationCode.CreateRandomCode(CodeLength); if (verificationCode == "") return; VerificationCode.CreateImage(verificationCode, picVCode); } 验证验证码是否正确

获取用户输入的验证码判断是否正确

private bool CaptchaVerification()//Captcha verification { if (String.IsNullOrEmpty(txbVCode.Text.Trim()) != true) { if (txbVCode.Text.Trim().ToLower() == verificationCode.ToLower()) return true; else { MessageBox.Show("验证码错误", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); UpdateVerificationCode(); txbVCode.Text = ""; txbVCode.Focus(); return false; } } else { MessageBox.Show("请输入验证码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); UpdateVerificationCode(); txbVCode.Text = ""; txbVCode.Focus(); return false; } } 4.保存账号及密码功能的实现 在APP.config中先创建键值对用于储存复选框状态以及记录账号信息 记录账号信息的函数 private void Remenberme()//Write user information { string loginID = txbID.Text.Trim(); string passwd = txbPwd.Text.Trim(); Configuration cf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (chBRemenberMe.Checked)//Write { cf.AppSettings.Settings["loginID"].Value = loginID; cf.AppSettings.Settings["passwd"].Value = passwd; cf.AppSettings.Settings["remenberme"].Value = "true"; } else//initialize { cf.AppSettings.Settings["loginID"].Value = ""; cf.AppSettings.Settings["passwd"].Value = ""; cf.AppSettings.Settings["remenberme"].Value = "false"; } cf.Save(); } 在窗体加载的同时写入储存的登录信息 private void Login_Load(object sender, EventArgs e)//Read user information from App.config { if (ConfigurationManager.AppSettings["remenberme"].Equals("true")) { txbID.Text = ConfigurationManager.AppSettings["loginID"]; txbPwd.Text = ConfigurationManager.AppSettings["passwd"]; chBRemenberMe.Checked = true; } } 5.登录功能 进入系统功能 private void EnterSystem() { DialogResult = DialogResult.OK; Remenberme(); } 点击按钮 private void btnLogin_Click(object sender, EventArgs e)//Determine whether to log in { logonCount += 1; if (logonCount public partial class Login : Form { int roleID = 0; int logonCount = 0; private const int CodeLength = 4; private String verificationCode = ""; public Login() { InitializeComponent(); } private void btnLogin_Click(object sender, EventArgs e)//Determine whether to log in { logonCount += 1; if (logonCount DialogResult = DialogResult.OK; Remenberme(); } private void btnExit_Click(object sender, EventArgs e)//Exit System { System.Environment.Exit(0); } private void Login_Load(object sender, EventArgs e)//Read user information from App.config { if (ConfigurationManager.AppSettings["remenberme"].Equals("true")) { txbID.Text = ConfigurationManager.AppSettings["loginID"]; txbPwd.Text = ConfigurationManager.AppSettings["passwd"]; chBRemenberMe.Checked = true; } } private void UpdateVerificationCode()//Creat / update verification { verificationCode = VerificationCode.CreateRandomCode(CodeLength); if (verificationCode == "") return; VerificationCode.CreateImage(verificationCode, picVCode); } private bool CaptchaVerification()//Captcha verification { if (String.IsNullOrEmpty(txbVCode.Text.Trim()) != true) { if (txbVCode.Text.Trim().ToLower() == verificationCode.ToLower()) return true; else { MessageBox.Show("验证码错误", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); UpdateVerificationCode(); txbVCode.Text = ""; txbVCode.Focus(); return false; } } else { MessageBox.Show("请输入验证码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); UpdateVerificationCode(); txbVCode.Text = ""; txbVCode.Focus(); return false; } } private void Remenberme()//Write user information { string loginID = txbID.Text.Trim(); string passwd = txbPwd.Text.Trim(); Configuration cf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (chBRemenberMe.Checked)//Write { cf.AppSettings.Settings["loginID"].Value = loginID; cf.AppSettings.Settings["passwd"].Value = passwd; cf.AppSettings.Settings["remenberme"].Value = "true"; } else//initialize { cf.AppSettings.Settings["loginID"].Value = ""; cf.AppSettings.Settings["passwd"].Value = ""; cf.AppSettings.Settings["remenberme"].Value = "false"; } cf.Save(); } private bool loginFlag()//Verify the user information,record roleID and return true or false { if (txbID.Text == "" || txbPwd.Text == "") { MessageBox.Show("用户名和密码不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } else {//Query user information string sql = "select * from Staff where LoginID = '" + txbID.Text + "' and Password = '" + txbPwd.Text + "'"; DataTable dataTable = SqlHelper.ExecuteTable(sql); if (dataTable.Rows.Count > 0) { DataRow dataRow = dataTable.Rows[0]; roleID = (int)dataRow["RoleID"]; Configuration cf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); cf.AppSettings.Settings["roleID"].Value = roleID.ToString();//record roleID cf.Save(); return true; } else { MessageBox.Show("用户名或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } } private void picVCode_Click(object sender, EventArgs e)//Click the image to update the verification code { if (logonCount>=3) UpdateVerificationCode(); } } } 五、运行效果

运行效果

后记

如果你这篇文章还不错的话,求点赞求收藏求转发,最重要的是你的支持是我做记录的最大动力,非常感谢!!!



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有