实现简单的页面登录和验证码验证

在一个程序中无论该程序是大还是小几乎都是要通过登录后才能体验其更多的功能,否则你所体验的只是其一部分功能,又很多操作是通过账号登录程序才能进行和体验的。
而登录时不仅需要对账号和密码进行验证,还有可能是要生成验证码进行验证,并且可切换验证码
生成验证码:

     /// 生成验证码(以图片形式)
        public ActionResult CreateValidCodeImage()
        {
            //1,随机生成长度为6的验证码字符串
            string strRandom = ValidCodeUtils.GetRandomCode(6);
            //2,根据生成的验证码字符串生验证码图片 byte数组类型
            byte[] imgByte = ValidCodeUtils.CreateImage(strRandom);
            //3,将验证码字符串存入Session validCode与存放验证码的HTML元素name值相同
            Session["validCode"] = strRandom;
            //4,把验证码图片以File形式返回到视图
            return File(imgByte, @"image/jpeg");
         }

File属性:File 文件格式 (参数1:具体要返回的东西-必须是byte[]数组类型,参数2:要返回的文件是什么东西)
在浏览器有Session和Cookie两种缓存机制,Cookie用于客户端,Session用于服务器,
都是用来记录信息,Session是在Cookie后才出现

用到封装的ValidCodeUtils方法

    public static class ValidCodeUtils
    {
       // 获得随机字符串  intLength长度
        public static string GetRandomCode(int intLength)
        {
            /*产生数字和密码混合的随机数*/
            string strReturn = string.Empty;
            Random random = new Random();//随机数
            for (int i = 0; i < intLength; i++){
                char cRerult;
                int intRandom = random.Next();//产生一个非负随机整数
                if (intRandom % 3 == 0)
                { //产生数字
                    cRerult = (char)(0x30 + (intRandom % 10));
                }
                else if (intRandom % 3 == 1)
                { //位数产生大写字母:大写字符 65-97 A 65
                    cRerult = (char)(0x41 + (intRandom % 0x1a));
                }
                else
                {
                    cRerult = (char)(0x61 + (intRandom % 0x1a));
                }
                strReturn += cRerult.ToString();
            }
            return strReturn;
        }
        /// 根据字符串创建验证码
        public static byte[] CreateImage(string strRandom)
        {
            //新增图片
            Bitmap newBitmap = new Bitmap(strRandom.Length * 20, 38);
            Graphics g = Graphics.FromImage(newBitmap);
            g.Clear(Color.White);
            //在图片上绘制文字
            SolidBrush solidBrush = new SolidBrush(Color.Red);
            g.DrawString(strRandom, new Font("Aril", 18), solidBrush, 12, 4);
            //在图片上绘制干扰线
            Random random = new Random();
            for (int i = 0; i < 10; i++){
                //产生一条线,并绘制到画布。 起始点(x,y)  总结点
                int x1 = random.Next(newBitmap.Width);
                int y1 = random.Next(newBitmap.Height);
                int x2 = random.Next(newBitmap.Width);
                int y2 = random.Next(newBitmap.Height);
                g.DrawLine(new Pen(Color.DarkGray), x1, y1, x2, y2);
            } 
            for (int i = 0; i < 100; i++)     //绘制图片的前景干扰点
            {
                int x = random.Next(newBitmap.Width);
                int y = random.Next(newBitmap.Height);
                newBitmap.SetPixel(x, y, Color.FromArgb(random.Next()));
            }
            g.DrawRectangle(new Pen(Color.Blue), 0, 0, newBitmap.Width, newBitmap.Height);
            g.DrawRectangle(new Pen(Color.Blue), -1, -1, newBitmap.Width, newBitmap.Height);
            //将图转保存到内存流(IQ流)中
            MemoryStream ms = new MemoryStream();
            newBitmap.Save(ms, ImageFormat.Jpeg);
            return ms.ToArray();//将流内容写入byte数组返回
        }
    }

登录:

      /// 验证登录 
        public ActionResult UserVerifyLogin(S_Users SUsers)
        {
            string strMsg = "fail";//记录状态
            string strAccount = SUsers.Account;//获取页面账号
            string strPassword = SUsers.Password;//获取页面密码
            string strValidCode = Request["validCode"];//获取验证码
            string strIsRember = Request["rememberpassword"];//记住我  
            //判断必要数据是否为空
            if (strAccount != "" && strPassword != "" && strValidCode != "")
            {
                string SessionValidCode = "";//存放验证码图片
                try
                { //记录页面传输过来的验证码
                    SessionValidCode = Session["validCode"].ToString();
                }
                catch (Exception e)
                { Console.WriteLine(e); }
                //判断验证码是否正确,StringComparison属性是忽略大小写
                if (SessionValidCode.Equals(strValidCode.Trim(), StringComparison.InvariantCultureIgnoreCase))
{
                    try
                    {
                        //判断账号是否存在
                        S_Users dbUser = (from tbUser in myModels.S_Users
                                          where tbUser.Account == strAccount
                                          && tbUser.ToVoidNo == true
                                          select tbUser).Single();
                        //使用AES256位加密方法对密码进行加密
                        string Password = AESEncryptHelper.Encrypt(strPassword);
                        /密码不加密的代码:
                        string Password  = strPassword;
                        //判断密码是否正确
                        if (dbUser.Password == Password)
                        {
                            //判断是否在页面记录账号和密码
                            if (strIsRember == "true")
                            {
                                //记住密码 保存cookie
                                //创建cookie对象
                                HttpCookie cookie = new HttpCookie("user");
                                //设置cookie里面对应的属性
                                cookie.Expires = DateTime.Now.AddDays(7);//保存7天
                                cookie["Account"] = strAccount;//用户名
                                cookie["Password"] = strPassword;//密码
                                Response.Cookies.Add(cookie);//放进Cookie里面 把记录显示出来
                            }
                            else
                            {
                                //删除 cookie机制
                                HttpCookie cookie = new HttpCookie("user");
                                cookie.Expires = DateTime.Now.AddDays(-1);
                                //通过设置Cookie 的过期时间为负数,强制使Cookie过期,
                                Response.Cookies.Add(cookie);
                            }
                            strMsg = "success";//登录成功
                        }
                        else
                        {
                            strMsg = "密码错误!";
                        }
                    }
                    catch (Exception)
                    { strMsg = "账号不存在!";}
                }
                else
                { strMsg = "验证码错误!";}
            }
            else
            { strMsg = "请填写完整的数据!";}
            return Json(strMsg, JsonRequestBehavior.AllowGet);
        }

JS:
在这里插入图片描述

页面:
在这里插入图片描述
对于密码,在一般情况在编写程序时,对于用户注册的账号的密码,通常会对其进行加密处理,
以防止程序被攻击时,泄漏用户的真正的账号信息,但又加密就会有加密,
如果知道其是何种加密方式,就和以对用户信息进行解密。
上面用到的是AES2556位的加密方法:(下次写)
AES256位:
AES-256:密钥长度为256位的高级加密标准
高级加密标准(英语:Advanced Encryption Standard,缩写:AES),
在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。
这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,
高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,
并在2002年5月26日成为有效的标准。
2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。

  • 6
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值