2019-6-4 Asp.net MVC4 权限过滤器及动作过滤器及结果过滤器等快速Demo

第一步:

创建C#,MVC4,基本项目,名称 FilterTest

第二步:

修改Web.config文件,如下图:

第三步:

在Models目录下,增加两个类文件:

 MyAuthorizeAttribute.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FilterTest.Models
{
    public class MyAuthorizeAttribute:AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            //return base.AuthorizeCore(httpContext);
            return DateTime.Now.Minute % 2 == 0;
        }
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            //base.HandleUnauthorizedRequest(filterContext);]
            filterContext.HttpContext.Response.Redirect("/Home/ShowTip");
        }
    }
}

 MyActionAttribute.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FilterTest.Models
{
    public class MyActionAttribute:ActionFilterAttribute
    {
        public string Para { get; set; }

        public override void OnActionExecuted(ActionExecutedContext filterContext)        {

            filterContext.HttpContext.Response.Write("<br/>" + Para + ":OnActionExecuted");
            base.OnActionExecuted(filterContext);
        }


        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Write("<br/>" + Para + ":OnActionExecuting");
            base.OnActionExecuting(filterContext);
        }


        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            filterContext.HttpContext.Response.Write("<br/>" + Para + ":OnResultExecuted");
          base.OnResultExecuted(filterContext);
        }


        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Write("<br/>" + Para + ":OnResultExecuting");
            base.OnResultExecuting(filterContext);
        }
    }
}

第四步:

创建Home控制器文件

HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FilterTest.Models;

namespace FilterTest.Controllers
{
    public class HomeController : Controller
    {
       
        public ActionResult Index()
        {        
            return View();
        }
        [Authorize(Users = "user")]
        public ActionResult UserPage()
        {
            return View();
        }
        [Authorize(Users = "admin")]
        [MyAuthorizeAttribute]
        public ActionResult AdminPage()
        {
            return View();
        }      
        public ActionResult ShowTip()
        {
            return View();
        }
        [MyAction(Para = "参数")]
        public string MyTest()
        {
            Response.Write("<br/>Action 执行中...");
            return "<br/>Action 返回结果";
        }
    }
}

创建Account控制器

AccountController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace FilterTest.Controllers
{
    public class AccountController : Controller
    {
        //
        // GET: /Account/

        public ActionResult Index(string ReturnUrl)
        {
            ViewBag.ReturnUrl = ReturnUrl;
            return View();
        }
        [Authorize]
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return Redirect("/Home/Index");
        }

        [HttpPost] 
        public ActionResult Login(string UserName, string PassWord, string ReturnUrl)
        { 
            bool result = FormsAuthentication.Authenticate(UserName,PassWord);
            if(result)
            { 
                FormsAuthentication.SetAuthCookie(UserName,false); 
                return Redirect(ReturnUrl??Url.Action("Index","Home"));
            } 
            else
            { 
                ModelState.AddModelError("","Incorrect UserName or PassWord"); 
                return View();
            }
        }

    }
}

第五步:

创建相应的View:

Accout控制器Index动作相对应的View

Index.csthml:

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
      @using (Html.BeginForm("Login", "Account", new {ReturnUrl = ViewBag.ReturnUrl },FormMethod.Post))    
    { 
        @Html.ValidationSummary() 
        <p><label>UserName:</label>
        <input name="UserName"/></p> 
        <p><label>Password:</label>
        <input name="PassWord"/></p> 
        <input type="submit" value="Log in"/>
 }
</body>
</html>

Home控制器使用的View:

Index.csthml:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
@{
    if (User.Identity.Name != null)
    {
        if(!User.Identity.Name.IsEmpty())
        {
          <h2>登陆者:@User.Identity.Name</h2>
        }
    }        
}
 <h2>@Html.ActionLink("Login","Index","Account")</h2>
 <h2>@Html.ActionLink("UserPage", "UserPage", "Home")</h2>
  <h2>@Html.ActionLink("AdminPage", "AdminPage", "Home")</h2> 
  <h2>@Html.ActionLink("Logout","Logout","Account")</h2>
</body>
</html>

UserPage.cshtml:
 

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>User</title>
</head>
<body>
    <h2>User 已登录.</h2>
</body>
</html>

AdminPage.cshtml:

@{
    ViewBag.Title = "admin";
}

<h2>admin 已登录.</h2>

ShowTip.cshtml:

@{
    ViewBag.Title = "ShowTip";
}

<h2>ShowTip:时间分值未到偶数.</h2>

总结:上述代码涉及到了:自定义权限过滤器,自定义动作过滤器,自定义结果过滤器.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gCodeTop 格码拓普 老师

您的鼓励.我的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值