博客
关于我
强烈建议你试试无所不能的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

你可能感兴趣的文章
阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
查看>>
阶段3 2.Spring_04.Spring的常用注解_3 用于创建的Component注解
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>