1、登录页面代码:

@{
ViewBag.Title = "会员登录";
Layout = "~/Views/Shared/_LayoutDialog.cshtml";
} <div class="loginBox">
<div class="loginHead" >
会员登录
</div>
<form id="hgl-form" class="form-horizontal" action="@Url.Action("Login", "Home", new { area = string.Empty })" method="post">
<div class="control-group">
<label for="inputEmail">账户</label>
<input type="text" name="account" id="inputEmail" class="validate[required]" />
</div>
<div class="control-group">
<label for="inputPassword">密码</label>
<input type="password" name="password" id="inputPassword" class="validate[required]" />
</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,此时可以看到页面跳转到了登录页面

MVC之ActionFilterAttribute的更多相关文章

  1. MVC中ActionFilterAttribute用法并实现统一授权

    MVC中ActionFilterAttribute经常用来处理权限或者统一操作时的问题. 先写一个简单的例子,如下: 比如现在有一个用户管理中心,而这个用户管理中心需要登录授权后才能进去操作或浏览信息 ...

  2. mvc通过ActionFilterAttribute做登录检查

    1.0 创建Attribute using System; using System.Collections.Generic; using System.Linq; using System.Web; ...

  3. 创建一个ASP.NET MVC OutputCache ActionFilterAttribute

    在每一个web应用程序中, 有的情况下,你想在一段时间内缓存一个具体的页面HTML输出,因为相关的数据和处理并不是总是变化.这种缓存的响应是储存在服务器的内存中.因为没有必要的额外处理,它提供了非常快 ...

  4. ASP.NET MVC 利用ActionFilterAttribute来做权限等

    ActionFilterAttribute是Action过滤类,该属于会在执行一个action之前先执行.而ActionFilterAttribute是 MVC的一个专门处理action过滤的类.基于 ...

  5. MVC 过滤器 ActionFilterAttribute

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

  6. ASP.NET MVC 通过ActionFilterAttribute来实现防止重复提交

    实现思想:每个页面打开的时候会在页面的隐藏控件自动生成一个值并将这个值赋值session,当提交方法的时候会在过滤器的时候进行获取session和页面传值过来的隐藏控件的值进行比较,如果值相同的话,重 ...

  7. MVC之ActionFilterAttribute自定义属性

    ActionFilterAttribute里有OnActionExecuting方法,跟Controller一样, 同是抽象实现了IActionFilter接口. // 登录认证特性 public c ...

  8. MVC过滤器-->ActionFilterAttribute和HandleErrorAttribute

    自定义的action过滤器  需要继承自ActionFilterAttribute 接口 OnActionExecuting:  在方法执行之前执行 OnActionExecuted:  方法的逻辑代 ...

  9. mvc 4 ActionFilterAttribute 特性,进行权限验证

    权限验证: /// <summary> /// 管理员身份验证 /// </summary> public class BasicAuthenticationAttribute ...

随机推荐

  1. esp8266 SOC方案经过半年沉淀之后再度重启二

    2018-08-2014:16:10 以下是输出控制 PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0);      GPIO_OUTPUT_SET ...

  2. C# 客户端读取共享目录文件

    控制台应用程序 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  3. ios 布局 素材 待整理

    https://www.cnblogs.com/fxwl/p/5961372.html div区域 8.盒子模型的相关属性 margin(外边距/边界) border(边框) padding(内边距/ ...

  4. Understanding and Analyzing Application Crash Reports

    Introduction When an application crashes, a crash report is created and stored on the device. Crash ...

  5. 梦想CAD控件COM接口搜索图面上的文字

    点击此处下载演示实例 主要用到函数说明: _DMxDrawX::NewSelectionSet 实例化一个构造选择集进行过滤,该类封装了选择集及其处理函数. _DMxDrawX::NewResbuf ...

  6. Linux下查看CPU信息、机器型号等硬件信息命令

    Linux下查看CPU信息.机器型号等硬件信息命令 编写一个bash脚本: vim info.sh #!/bin/bash cat /etc/issue echo "____________ ...

  7. 重启rsyncd

    systemctl  restart  rsyncd.service

  8. jquery 实现点评标签 类似淘宝大众点评的 快速准时 货品完好等

    111 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...

  9. 还没更换RubyGems镜像?

    相信用过Ruby的人都知道 gem install 命令,但是在国内该命令安装的速度甚是不稳定(你懂的),导致尝试数次便是等待数时,记得之前在安装redmine的时候便是如此,之前不懂什么意思,还以为 ...

  10. 60s倒计时

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...