asp.net MVC4 ActionFilterAttribute过滤器使用,主要处理当用户不登陆的时候打开页面跳转到登录页面功能

1、登录页面代码:

@{
    ViewBag.Title = "会员登录";
    Layout = "~/Views/Shared/_LayoutDialog.cshtml";
}

<div class="loginBox">
    <div class="loginHead" >
        会员登录
    </div>
    <form >
        <div class="control-group">
            <label for="inputEmail">账户</label>
            <input type="text" name="account"  />
        </div>
        <div class="control-group">
            <label for="inputPassword">密码</label>
            <input type="password" name="password"  />
        </div>
        @*<div class="control-group" >
                    <label class="checkbox">
                        <input type="checkbox" name="rememberMe" checked>
                        记住我</label>
                </div>*@
        <div class="form-actions">
            <button type="submit" class="btn btn-block">登录</button>
        </div>
    </form>

</div>
@section scripts{
<script type="text/javascript">
$(function () {
//表单提交
hgl.sumbit(function () {
location.href = '@Url.Action("Index", "SiteSet", new { area = "Setting" })';//登陆成功后跳转到的页面
});
})
</script>
}

  2、登录controller代码:

        //会员登录
        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(string account, string password)
        {
            var entity = AdminService.QueryDetailForAccount(account);
            if (entity == null)
                return JRFaild("all", "此账户不存在");

            if (entity.Password != password.ToMD5())
                return JRFaild("all", "账户密码输入错误,请重新输入");

            if (entity.Freezed == 1)
                return JRFaild("all", "此账户已被冻结,暂不能登录,请联系超级管理员");

            var result = AdminService.Login(account, password, entity);

            if (result)
            {
                Session["account"] = account;
Session["guid"] = entity.Guid;
Session["username"] = entity.Name;
Session["password"] = password; return JRSuccess("登录成功"); } return JRFaild("all", "登录失败,用户名或密码错误或账户不存在"); }

  前面是登录模块的代码;下面主要介绍 ActionFilterAttribute过滤器的使用

1、在项目中新建个Filter文件夹,在文件夹里面添加该过滤器类,命名为:BasicAuthAttribute.cs;此类需要继承ActionFilterAttribute(关于ActionFilterAttribute大家可以按F12跳转到该类的详细介绍进行了解和使用);我在新建的BasicAuthAttribute.cs中使用了OnActionExecuting,该类代码如下:如下代码在使用的时候还需要添加引用:

using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
namespace Linkin.Manager.Filter
{
    public class BasicAuthAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var account = filterContext.HttpContext.Session["account"];
var password = filterContext.HttpContext.Session["password"];
if (account == null || password == null) { //用户不登陆的时候跳转到登录页面 filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Home", action = "Login", area = string.Empty })); } } } }

到此为止,过滤器已经写好了,具体的项可以直接F12到起定义进行查看,可以看出当用户不登陆的时候会跳转到登录页面

2、此时需要到配置文件里的登录配置是怎么配置的,打开web.config,找到节点<authentication mode="Forms"></authentication>查看配置,如果自己的登录页面跟web.config里面的配置一样就不需要修改了,不一致的最好修改一下

    <!--这里配置的是登录页面的权限配置-->
    <authentication mode="Forms">
      <forms loginUrl="~/Home/Login" timeout="2880"   />
    </authentication>

3、这样以后在controller里面就可以直接用该过滤器了,在要使用此项过滤器的controller里面直接加入下面的红色字体,此时这样还需要引入该文件的引用:using Linkin.Manager.Filter;(此引用要根据自己的项目的实际情况来添加)代码如下:

   [BasicAuthAttribute]
    public class AdminController : BasicController
    {
        [HttpGet]
        public ActionResult Index(string id, string key, int state = -1, int page = 1)
        {
            ViewBag.Id = id;
            ViewBag.Key = key;
            ViewBag.State = state;
            return View(AdminService.QueryPageList(id, key, state, page, 10));
        }
   }

上面的代码是将起放到了外面,也可以将起直接放到里面,如下:

public class AdminController : BasicController
    {
        [BasicAuthAttribute]
        [HttpGet]
        public ActionResult Index(string id, string key, int state = -1, int page = 1)
        {
            ViewBag.Id = id;
            ViewBag.Key = key;
            ViewBag.State = state;
            return View(AdminService.QueryPageList(id, key, state, page, 10));
        }
  }

4、经过以上的步骤就弄好了,此时运行网站,在不登陆的时候,直接在浏览器的地址栏输入http://localhost:2341/setting/admin,此时可以看到页面跳转到了登录页面