博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
3、VS2010+ASP.NET MVC4+EF4+JqueryEasyUI+Oracle项目开发之——用户登录
阅读量:4324 次
发布时间:2019-06-06

本文共 6720 字,大约阅读时间需要 22 分钟。

近期因为项目赶着上线,一直没时间接着写博客,今天最终空出了时间。声名:我不是专业美工,所以界面问题,希望大家不要拍砖。登录界面例如以下:

在ASP.NET MVC中,要新增一个功能,我们首先要加入一个控制器,AccountController.cs,加入方法:右键单击Controllers目录,

///         /// 登录页面        ///         /// 
public ActionResult Index() { return View(); } /// /// 点击 登录系统 后取消 /// /// 登录信息 ///
[HttpPost] public ActionResult Index(LogOnModel model) { #region 验证码验证 if (Session["_VCode"] != null && model.ValidateCode!=null&& model.ValidateCode.ToLower() != Session["_VCode"].ToString()) { ModelState.AddModelError("PersonName", "验证码错误!"); //return ""; return View(); } #endregion if (ModelState.IsValid) //这个是界面数据的模型验证 相应LogOnModel模型类中 诸如:[Required(ErrorMessage = "请填写username")] 必填 ...等验证 { SMUSERTB person = _accountService.ValidateUser(model.PersonName, xEncrypt.EncryptText(model.Password)); if (person != null) //登录成功 { Account account = new Account(); account.USER_NAME = person.USER_NAME; account.UID = person.U_ID; account.USER_ID = person.USER_ID; account.GuidCode = person.GUILD_CODE; Session["account"] = account; return RedirectToAction("Index", "Home"); } } ModelState.AddModelError("PersonName", "username或者password出错。"); return View(); }   public void ValidateCode()        {            Response.ClearContent(); //须要输出图象信息 要改动HTTP头             Response.ContentType = "image/jpeg";            ValidateCodeFun.CreateValidateCode(4);        }
当中LogOnModel是专门为登录进行设计的业务模型,

public class LogOnModel    {        [Required(ErrorMessage = "请填写username")]        [DisplayName("username")]        public string PersonName { get; set; }        [StringLength(100, ErrorMessage = "{0} 必须至少包括 {2} 个字符。", MinimumLength = 3)]        [Required(ErrorMessage = "请填写password")]        [DataType(DataType.Password)]        [DisplayName("password")]        public string Password { get; set; }        [Required(ErrorMessage = "请填写验证码")]        [DisplayName("验证码")]        public string ValidateCode { get; set; }        [DisplayName("记住我?")]        public bool RememberMe { get; set; }    }

关于验证码类,我就不多说了,大家都懂,我这里直接贴代码,例如以下:

public static class ValidateCodeFun    {        public static void CreateValidateCode(int codeNum)        {            string vnum= GetByRndNum(codeNum);            Bitmap Img = null;            Graphics g = null;            Random random = new Random();            int gheight = vnum.Length * 16;            Img = new Bitmap(gheight, 26);            g = Graphics.FromImage(Img);            //Font f = new Font("微软雅黑", 16, FontStyle.Bold);            Color[] c = { Color.SpringGreen, Color.Red, Color.LightBlue, Color.SeaGreen, Color.Orange, Color.Yellow, Color.RosyBrown };            Font f = new Font("宋体", 16, FontStyle.Bold);            g.Clear(Color.White);//设定背景色            //Pen blackPen = new Pen(ColorTranslator.FromHtml("#e1e8f3"), 18);            Pen blackPen = new Pen(c[random.Next(7)], 18);            //绘图片的前景噪音点            for (int i = 0; i < 80; i++)            {                int x = random.Next(Img.Width);                int y = random.Next(Img.Height);                Img.SetPixel(x, y, Color.FromArgb(random.Next()));            }            for (int i = 0; i < 128; i++)// 随机产生干扰线,使图象中的认证码不易被其他程序探測到            {                int x = random.Next(gheight);                int y = random.Next(20);                int xl = random.Next(6);                int yl = random.Next(2);                g.DrawLine(blackPen, x, y, x + xl, y + yl);            }            SolidBrush s = new SolidBrush(ColorTranslator.FromHtml("#411464"));            g.DrawString(vnum, f, s, 1, 1);            //画边框            blackPen.Width = 1;            g.DrawRectangle(blackPen, 0, 0, Img.Width - 1, Img.Height - 1);            Img.Save(HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg);            s.Dispose();            f.Dispose();            blackPen.Dispose();            g.Dispose();            Img.Dispose();            //Response.End();        }        //-----------------给定范围获得随机颜色        static Color GetByRandColor(int fc, int bc)        {            Random random = new Random();            if (fc > 255)                fc = 255;            if (bc > 255)                bc = 255;            int r = fc + random.Next(bc - fc);            int g = fc + random.Next(bc - fc);            int b = fc + random.Next(bc - bc);            Color rs = Color.FromArgb(r, g, b);            return rs;        }        //取随机产生的认证码(数字)        public static string GetByRndNum(int VcodeNum)        {            string VNum = "";            Random rand = new Random();            for (int i = 0; i < VcodeNum; i++)            {                VNum += VcArray[rand.Next(0, 61)];            }            HttpContext.Current.Session["_VCode"] = VNum.ToLower();            return VNum;        }        private static readonly string[] VcArray =            new string[] {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i",                "j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G",                "H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z" };    }
最后,看下视图View,新建View方法例如以下:

View十分简单,这里我就不多说,自己看代码,例如以下:

@model YKT.Model.LogOnModel@{    Layout = null;}    企业平台登录    
@using (Html.BeginForm((string)ViewBag.FormAction, "Account")) {
}

转载于:https://www.cnblogs.com/mengfanrong/p/3770066.html

你可能感兴趣的文章
android驱动在win10系统上安装的心酸历程
查看>>
优雅的程序员
查看>>
oracle之三 自动任务调度
查看>>
Android dex分包方案
查看>>
ThreadLocal为什么要用WeakReference
查看>>
删除本地文件
查看>>
FOC实现概述
查看>>
base64编码的图片字节流存入html页面中的显示
查看>>
这个大学时代的博客不在维护了,请移步到我的新博客
查看>>
GUI学习之二十一——QSlider、QScroll、QDial学习总结
查看>>
nginx反向代理docker registry报”blob upload unknown"解决办法
查看>>
gethostbyname与sockaddr_in的完美组合
查看>>
kibana的query string syntax 笔记
查看>>
旋转变换(一)旋转矩阵
查看>>
thinkphp3.2.3 bug集锦
查看>>
[BZOJ 4010] 菜肴制作
查看>>
C# 创建 读取 更新 XML文件
查看>>
KD树
查看>>
VsVim - Shortcut Key (快捷键)
查看>>
C++练习 | 模板与泛式编程练习(1)
查看>>