统一登录验证:

1、定义实体类Person:利用特性标签验证输入合法性设计登录页面

1
2
3
4
5
6
7
8
9
public class Person
{
    [DisplayName("用户名"), Required(ErrorMessage = "账户非空!")]
    public string LoginName { getset; }
    [DisplayName("密 码"), Required(ErrorMessage = "密码非空!")]
    public string Password { getset; }
    [DisplayName("验证码"), Required(ErrorMessage = "验证码非空!")]
    public string Vcode { getset; }
}

2、设计验证码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class VcodeController : Controller
{
    //
    // GET: /Vcode/
 
    public ActionResult Vcode()
    {
        byte[] buffer;
        string str = GetRdStr(5);
        //将生成随机字符串存入session中
        Session["vcode"] = str;
        using(Image img=new Bitmap(80,30))
        {
            using(Graphics g=Graphics.FromImage(img))
            {
                //清除背景色
                g.Clear(Color.White);
                g.DrawRectangle(Pens.Black, 0, 0, img.Width - 1, img.Height - 1);
                DrawLines(50, img, g);
                g.DrawString(str, new Font("微软雅黑", 16), Brushes.Black, 0, 0);
                DrawLines(35, img, g);
            }
            using(System.IO.MemoryStream ms=new System.IO.MemoryStream())
            {
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                buffer=ms.ToArray();
            }
        }
        return File(buffer,"image/jpeg");
    }
    /// <summary>
    /// 定义随机对象
    /// </summary>
    Random rd = new Random();
 
    /// <summary>
    /// 生产指定长度随机字符串
    /// </summary>
    /// <returns>随机字符串</returns>
    string GetRdStr(int count)
    {
        string str = "abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ23456789";
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        for(int i=0;i<count;i++)
        {
            sb.Append(str[rd.Next(str.Length)]);
        }
        return sb.ToString();
    }
    /// <summary>
    /// 绘制指定数量干扰线
    /// </summary>
    /// <param name="count">数量</param>
    /// <param name="img">图片对象</param>
    /// <param name="g">画家对象</param>
    void DrawLines(int count,Image img,Graphics g)
    {
        for(int i=0;i<count;i++)
        {
            Point p1 = new Point(rd.Next(img.Width - 1), rd.Next(img.Height - 1));
            Point p2 = new Point(p1.X + rd.Next(-1, 2), p1.X + rd.Next(-1, 2));
            g.DrawLine(Pens.AliceBlue, p1, p2);
        }
    }
}

3、设计登录页面,并写入登录逻辑

前台页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
@{
    ViewBag.Title = "Login";
}
@using 统一登录2.Models
@model Person
 
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
@using (@Html.BeginForm("Login", "Login", FormMethod.Post))
{
    <table>
        <tr>
            <th>@Html.DisplayNameFor(p => p.LoginName)</th>
            <td>
                @Html.TextBoxFor(p => p.LoginName)
                @Html.ValidationMessageFor(p => p.LoginName)
            </td>
        </tr>
        <tr>
            <th>@Html.DisplayNameFor(p => p.Password)</th>
            <td>
                @Html.PasswordFor(p => p.Password)
                @Html.ValidationMessageFor(p => p.Password)
            </td>
        </tr>
        <tr>
            <th>@Html.DisplayNameFor(p => p.Vcode)</th>
            <td>
                @Html.TextBoxFor(p => p.Vcode)
                <img src="@Url.Action("Vcode", "Vcode")" />
                @Html.ValidationMessageFor(p => p.Vcode)
            </td>
        </tr>
        <tr>
            <th></th>
            <td>
                <input type="submit" value="登录" />
            </td>
        </tr>
    </table>
}
<script type="text/javascript">
 
</script>

后台逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class LoginController : Controller
{
    //
    // GET: /Login/
    [HttpGet]
    public ActionResult Login()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Login(Person model)
    {
        //判断实体验证是否成功
        if (ModelState.IsValid == false)
        {
            ModelState.AddModelError("""实体验证失败!");
            return View();
        }
        //判断验证码验证是否成功
        if(Session["vcode"]!=null&&Session["vcode"].ToString().Equals(model.Vcode,StringComparison.OrdinalIgnoreCase))
        {
            ModelState.AddModelError("""验证码输入不正确!");
            return View();
        }
        //判断账户密码验证是否成功
        if(model.LoginName!="admin"||model.Password!="123")
        {
            ModelState.AddModelError("""用户名或密码错误!");
            return View();
        }
        //登录成功,登录信息存入Session
        Session["uInfo"] = model;
        //跳转到主页面
        return RedirectToAction("Index""Home");
    }
 
}

4、在过滤器中写入统一验证逻辑--注:这里用标签来判断是否需要进行登录验证,标签类实在第五步定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    //判断是否需要登录验证
    if(filterContext.ActionDescriptor.IsDefined(typeof(IgnoreLoginAttribute),false))
    {
        return;
    }
    if (filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(IgnoreLoginAttribute), false))
    {
        return;
    }
    //根据Session进行登录验证
    if(filterContext.HttpContext.Session["uInfo"]==null)
    {
        //1.Ajax请求
        if(filterContext.HttpContext.Request.IsAjaxRequest())
        {
            JsonResult jsonView = new JsonResult();
            jsonView.ContentType = "application/json";
            jsonView.Data = new {status=2,msg="未登录" };
            jsonView.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            filterContext.Result = jsonView;
        }
        //2.一般请求
        else
        {
            ContentResult contentView = new ContentResult();
            contentView.Content = "<script>window.alert('您还没有登录!请重新登录...');window.location='/Login/Login'</script>";
            filterContext.Result = contentView;
        }
    }
}

5、定义自定义特性标签,不需要登录验证统一贴上特性标签

1
2
3
4
5
6
7
/// <summary>
/// 是否忽略登录验证特性标签
/// </summary>
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method,AllowMultiple=false)]
public class IgnoreLoginAttribute:Attribute
{
}

MVC过滤器进行统一登录验证的更多相关文章

  1. MVC过滤器实现用户登录验证

    前言当我们访问某个网站的时候需要检测用户是否已经登录(通过Session是否为null),我们知道在WebForm中可以定义一个BasePage类让他继承System.Web.UI.Page,重写它的 ...

  2. MVC前台页面做登录验证

    最近接触了一个电商平台的前台页面,需要做一个登录验证,具体情况是:当用户想要看自己的订单.积分等等信息,就需要用户登录之后才能查询,那么在MVC项目中我们应该怎么做这个前台的验证呢? 1.我在Cont ...

  3. asp.net mvc中的用户登录验证过滤器

    在WEB项目中建立 类:      public class LoginFilter : ActionFilterAttribute     {         public override voi ...

  4. mvc 3 Mvc 4 使用Forms 登录验证随笔一

    前言 本人虽然做 .Net 也有五年有余,可是没什么大才,总是干些打杂的活,技术很少差劲呀.以前不管是做内部管理系统,还是企业平台,保存用户登录信息用的都是Session,也许是从一开始就接触Sess ...

  5. MVC过滤器详解 面向切面编程(AOP)

    面向切面编程:Aspect Oriented Programming(AOP),面向切面编程,是一个比较热门的话题.AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个 ...

  6. [MVC学习笔记]5.使用Controller来代替Filter完成登录验证(Session校验)

          之前的学习中,在对Session校验完成登录验证时,通常使用Filter来处理,方法类似与前文的错误日志过滤,即新建Filter类继承ActionFilterAttribute类,重写On ...

  7. 过滤器实现Token验证(登录验证+过期验证)---简单的实现

    功能:登录验证+过期验证+注销清除cookie+未注销下关闭或刷新浏览器仍可直接访问action概述:token只存在客户端cookie,后端AES加密+解密+验证,每一次成功访问action都会刷新 ...

  8. asp.net MVC 过滤器使用案例:统一处理异常顺道精简代码

    重构的乐趣在于精简代码,模块化设计,解耦功能……而对异常处理的重构则刚好满足上述三个方面,下面是我的一点小心得. 一.相关的学习 在文章<精简自己20%的代码>中,讨论了异常的统一处理,并 ...

  9. C# mvc中为Controller或Action添加定制特性实现登录验证

    在本文开始前,先简单讲两个知识点: 1.每个action执行前都会先执行OnActionExecuting方法: 2.FCL提供了多种方式来检测特性的存在,比如IsDefined.GetCustomA ...

随机推荐

  1. Linux----函数中变量的作用域、local关键字。

    问题引出: 问题说明: shell的函数中如果变量的前面没有加local关键字.在全局作用域内存在这个变量名的情况下.函数中会直接使用这个变量 而不是自己创建一个.要想做到在函数创建一个变量也是可以的 ...

  2. Linux彩色输出

    在linux下,可以使用一些宏,加上自定义格式输出,让输出更易于调试: 排版出来可能有些乱,注意do{ }while(0);是在一行里就可以了. #include <stdio.h> #i ...

  3. Unix/Linux环境C编程入门教程(1) Solaris 11 64bit环境搭建

    Unix/Linux版本众多,我们推荐Unix/Linux初学者选用几款典型的Unix/Linux操作系统进行学习. 本文就带大家来安装Solaris 11 64位并且配置好C/C++开发环境 本文所 ...

  4. windows的命令行工具和DOS工具的区别

    很多的系统管理员可能认为命令行是程序员编程用的,这是不对的,其实命令行是另一种用来管理计算机的接口.1 命令行窗口        Windows NT/Windows 2000以后的操作系统为用户提供 ...

  5. 浅谈Web Api配合SignalR的跨域支持

    最近接手的一个项目中,涉及到一个简单的消息模块,由于之前有简单了解过SignalR,所以打算尝试着摸索摸索~! 首先,通过Nuget管理器添加Microsoft ASP.NET SignalR引用~目 ...

  6. EOF 空格问题

    mysql -u $USER -p${PASSWORD} $DATABASE << EOF >/tmp/dd-$$ 2>/tmp/ddd-$$select *from $TAB ...

  7. 04737_C++程序设计_第10章_面向对象设计实例

    10.6.2 使用包含的参考程序及运行结果. 头文件cpp10.h 源文件cpp10.cpp 源文件Find10.cpp 头文件cpp10.h #if ! defined(CPP10_H) #defi ...

  8. mongoose post方法总结and疑点

    官方文档代码: var schema = new Schema(..); schema.post('save', function (doc) { console.log('this fired af ...

  9. Android的MVC框架

    http://www.cnblogs.com/wanghafan/archive/2012/07/20/2600786.html MVC是当前比较流行的框架,随便Google下,就可以发现几乎所有的应 ...

  10. Android Gson深入分析

    眼下解析json有三种工具:org.json(Java经常使用的解析),fastjson(阿里巴巴project师开发的).Gson(Google官网出的).解析速度最快的是Gson,下载地址:htt ...