与ActionFilter相关的接口有2个:

□ IActionFilter 对action执行前后处理

void OnActionExecuting(ActionExecutingContext filterContext);
可以在此对请求处理,甚至开启一个新的请求。

void OnActionExecuted(ActonExecutedContext filterContext);
可以在此对返回结果处理,甚至取消返回结果。

关于参数ActionExecutingContext和ActonExecutedContext共有的:
都继承于ControllerContext。
都有ActionDescriptor属性:提供了action的细节
都有ActionResult属性:当设置为null的时候取消整个请求

关于ActonExecutedContext独有的:
Canceled属性:bool类型,ActionExecutedContext是否被其它action filter取消
Exception属性:action filter和action抛出的异常
ExceptionHandled属性:bool类型,异常是否被处理

□ IResultFilter 对action返回结果前后做处理

方法与属性与IActionFilter类似。
void OnResultExecuted(ResultExecutedContext filterContext);
void OnResultExecuting(ResultExecutingContext filterContext);

实例:继承ActionFilterAttribute为登录密码加密

ActionFilterAttribute包含了如下4个方法:
void OnActionExecuting(ActionExecutingContext filterContext);
void OnActionExecuted(ActonExecutedContext filterContext);
void OnResultExecuted(ResultExecutedContext filterContext);
void OnResultExecuting(ResultExecutingContext filterContext);
所以,我们可以在派生类中重写这4个方法。

□ 思路

→在执行action之前对密码加密
→在执行action之后,根据是否登录成功,来决定返回成功或重新登录视图
→在action返回结果之后,再追加一些内容

□ 继承ActionFilterAttribute

using System;
using System.Security.Cryptography;
using System.Text;
using System.Web.Mvc;
using System.Web.Security;
 
namespace MvcApplication1.Extension
{
    public class EncryptLoginAttribute : ActionFilterAttribute
    {
        private string username;
        private string password;
        private bool isAuthorized = false;
        private string longdate;
        private string lastTry;
 
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            username = filterContext.HttpContext.Request.Form["username"];
            password = filterContext.HttpContext.Request.Form["password"];
            MD5 md5Hash = MD5.Create();
            string md5Password = GetMD5Hash(md5Hash, password);
            bool result = Membership.ValidateUser(username, md5Password);
            if (result)
            {
                FormsAuthentication.SetAuthCookie(username, false);
                isAuthorized = true;
                longdate = DateTime.Now.ToLongDateString();
            }
            else
            {
                isAuthorized = false;
                lastTry = DateTime.Now.ToShortDateString() + "-" + DateTime.Now.ToShortTimeString();
            }
        }
 
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (isAuthorized)
            {
                filterContext.Result = new ViewResult(){ViewName = "Welcome"};
            }
            else
            {
                ViewResult result = new ViewResult();
                result.ViewName = "Index";
                result.ViewBag.message = "Login fail";
                filterContext.Result = result;
            }
        }
 
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            if (filterContext.Exception == null && !filterContext.Canceled)
            {
                ViewResult result = (ViewResult)filterContext.Result;
                if (result != null)
                {
                    if (result.ViewName == "Welcome")
                    {
                        filterContext.HttpContext.Response.Write("<p style='color:Green;'><br/>Today is "
+ longdate + "<br/></p>");
                    }
                    else if (result.ViewName == "Index")
                    {
                        filterContext.HttpContext.Response.Write("<p style='color:Red;'><br />Last Login attemp at "+lastTry+"<br/></p>");
                    }
                    filterContext.Result = result;
                }
            }
        }
 
        private static string GetMD5Hash(MD5 md5Hash, string input)
        {
            //string→byte[]
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
            StringBuilder stringBuilder = new StringBuilder();
            foreach (byte b in data)
            {
                stringBuilder.Append(b.ToString("x2"));
            }
            return stringBuilder.ToString();
        }
    }
}
 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

□ HomeController

using System.Web.Mvc;
using System.Web.Security;
using MvcApplication1.Extension;
 
namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
 
        [HttpPost]
        [EncryptLogin]
        public ActionResult Login(string username, string password)
        {
            //TODO:保存到数据库
            return null;
        }
 
        public ActionResult SignOut()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index");
        }
 
    }
}
 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

□ Home/Index.cshtml为登录页

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
 
<h2>Index</h2>
<p style="color: red;">@ViewBag.message</p>
@using (Html.BeginForm("Login", "Home", FormMethod.Post, new {id = "loginForm"}))
{
    <p>
        用户名:@Html.TextBox("username", null, new {style = "width:100px"})
    </p>
    <p>
        密码: @Html.Password("password", null, new {style = "width: 100px"})
    </p>
    <p>
        <input type="submit" name="login" value="登录"/>
    </p>
}
 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

□ web.config相关配置

<authentication mode="Forms">
      <forms loginUrl="~/Home/Index" timeout="2880">
        <credentials passwordFormat="Clear">
          <user name="name" password="21218cca77804d2ba1922c33e0151105"/>
        </credentials>
      </forms>
    </authentication>

□ 登录成功视图:/Shared/Welcome.cshtml

@{
    ViewBag.Title = "Welcome";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>登录成功~~</h2>
@Html.ActionLink("登出","SignOut","Home")

登录页:

登录失败:

□ 如果想在全局使用

filters.Add(new EncryptLoginAttribute());

□ 备注

暂没有把登录成功显示页面调试出来,因为,当使用FormsAuthentication.Authenticate(username, md5Password)时,提示此方法已经过时;而使用Membership.ValidateUser(username, md5Password)时,对应的Web.config如何配置,暂没细究。

参考资料:
MVC Filters Part 3 - Action Filter and Action Result Filter

MVC扩展Filter,通过继承ActionFilterAttribute为登录密码加密的更多相关文章

  1. MVC扩展Filter, 通过继承AuthorizationAttribute限制IP

    为什么需要AuthorizationAttribute 在没有Authorization系统属性之前,我们可能这样判断:Request.IsAuthenticated && User. ...

  2. MVC扩展Filter,通过继承HandleErrorAttribute,使用log4net或ELMAH组件记录服务端500错误、HttpException、Ajax异常等

    □ 接口 public interface IExceptionFilter{    void OnException(ExceptionContext filterContext);} Except ...

  3. 用RSA加密实现Web登录密码加密传输

    通常我们做一个Web应用程序的时候都需要登录,登录就要输入用户名和登录密码,并且,用户名和登录密码都是明文传输的,这样就有可能在中途被别人拦截,尤其是在网吧等场合. 这里顺带一个小插曲,我以前有家公司 ...

  4. laravel更改默认的登录密码加密方式

    laravel更改默认的登录密码加密方式   laravel 默认用的登录密码加密方式是: $password = Hash::make('password'); 而我平时用的密码加密方式是: $pa ...

  5. web登录密码加密

    文章:如何实现登录页面密码加密 文章:用RSA加密实现Web登录密码加密传输 文章:web登录用户名密码加密 知乎文章:Web前端密码加密是否有意义? 文章:记录一次黑客模拟攻击 成功拿到淘宝账号和密 ...

  6. 每日JS逆向练习之斗鱼登录密码加密,今天你练了吗?

    一切的基本功都是为后期调试滑块验证码准备的. 有兴趣的关注一下知识图谱与大数据公众号,当然不关注也无所谓.今天来看看斗鱼登录密码加密,正所谓熟能生巧,这种简单一点的基本3-5分钟就要能抠出来,有兴趣得 ...

  7. C#实现京东登录密码加密POST

    1.京东登录登录密码 function getEntryptPwd(pwd) { var pubKey = $('#pubKey').val(); if (!pwd || !pubKey || !Sy ...

  8. MVC扩展ModelBinder,通过继承DefaultModelBinder把表单数据封装成类作为action参数

    把视图省.市.街道表单数据,封装成一个类,作为action参数.如下: action方法参数类型: namespace MvcApplication1.Models{    public class ...

  9. shiro登录密码加密

    密码加密 String passwd = new SimpleHash("SHA-1", "username", "password").t ...

随机推荐

  1. elasticsearch学习笔记--原理介绍

    前言:上一篇中我们对ES有了一个比较大概的概念,知道它是什么,干什么用的,今天给大家主要讲一下他的工作原理 介绍:ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户 ...

  2. MySQL学习笔记:调用存储过程或函数报1418错误

    问题 MySQL开启bin-log后,调用存储过程或者函数以及触发器时,会出现错误号为1418的错误: ERROR 1418 (HY000): This function has none of DE ...

  3. 一步一步学习IdentityServer4 (2) 开始一个简单的事例

    前面将来一些配置,但是很多都不是必要的,先放一些事例需要的简要配置把 既然是IdentityServer4 所里下面的例子我 直接放到 Linux上 测试环境 CentOS 7 +Nginx1.9.3 ...

  4. 微商城三级分销源码公众号开发 微分销 C#源码

    需要源码,请加QQ:858-048-581 ,可以查看演示 运行环境:vs2012+ sql2008r2 [什么是微分销] 微分销是助力企业进军移动电商,完善分销体系搭建微信分销系统.基于微信平台,搭 ...

  5. 收集Nginx的json格式日志(五)

    一.配置nginx [root@linux-node1 ~]# vim /etc/nginx/nginx.conf #修改日志格式为json格式,并创建一个nginxweb的网站目录 log_form ...

  6. ECshop语言包lang的加载原理

    当前使用的ecshop的版本:2.7.3,ecshop 2.7.3版本的网店系统的语言包的位置是ecshop文件下 languages/xxx/   其中的xxx表示各种语言的文件夹,里面存放指定语言 ...

  7. .NET之类型转换

    说起类型转换大家很容易的就会联想到将int类型转换成float类型或者是将double类型转转成int类型之类的转换.当然这可能是大多数人最先接触到的转换方式,也是最简单的转换方式.所谓转换就是从现有 ...

  8. [MySQL-笔记]创建高性能索引

    索引,MySQL中也叫“键”,是存储引擎中用于快速找到记录的一种数据结构,具体的工作方式就像书本中的索引一样,但是具体的实现方式会有差别. 一.索引分类 B-Tree索引: 优点: MyISAM中,索 ...

  9. opencv 加载 修改 保存 图像

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; /* 1 加载图像 cv::imre ...

  10. 机器学习之路:python 字典特征提取器 DictVectorizer

    python3 学习使用api 将字典类型数据结构的样本,抽取特征,转化成向量形式 源码git: https://github.com/linyi0604/MachineLearning 代码: fr ...