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

你可能感兴趣的文章
【翻译】在Ext JS集成第三方库
查看>>
中华优秀传统文化教育的有效渗透
查看>>
计算机基础篇-01
查看>>
leetcode 58. Length of Last Word
查看>>
ios开发证书,描述文件,bundle ID的关系
查看>>
jsp之简单的用户登录系统(纯jsp)
查看>>
js计时事件
查看>>
EntityFramework 学习 一 Eager Loading
查看>>
dispatch_set_target_queue测试
查看>>
自己写的sql排序
查看>>
关于Mutex的笔记
查看>>
凸包1——卷包裹算法
查看>>
Centos 安装SVN并配置多个版本库
查看>>
Eos持久化实体
查看>>
Shell基础-通配符
查看>>
static类型的变量
查看>>
SpringMVC之文件上传异常处理
查看>>
优先级
查看>>
rest_framework组件
查看>>
css position relative obsolution
查看>>