ASP.NET MVC使用AuthenticationAttribute验证登录
首先,添加一个类AuthenticationAttribute,该类继承AuthorizeAttribute,如下:
using System.Web;
using System.Web.Mvc; namespace Zhong.Web
{
public class AuthenticationAttribute : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
//base.OnAuthorization(filterContext);
//如果控制器没有加AllowAnonymous特性或者Action没有加AllowAnonymous特性才检查
if (!filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute),true) && !filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute),true))
{
//此处写判断是否登录的逻辑代码
//这里使用cookie来判断是否登录,为了简单说明特性的使用方式,cookie记录的是用户名和明文密码(实际当中需要经过诸如加密等处理)
HttpCookie cookie = filterContext.HttpContext.Request.Cookies["Member"];
if (!(cookie!=null && cookie.Values["name"] == "test" && cookie.Values["pass"] == ""))
{
filterContext.Result = new RedirectResult("/Member/Login");
}
}
}
}
}
在MemberControll中加上特性Authentication,Member控制器下有三个Action方法,一个是首页Index,一个是登录页Login,一个是处理Post方式的登录页Login,Index对应的视图代码如下:
@{
ViewBag.Title = "Index";
}
<h2>这是会员中心</h2>
Login对应的视图代码如下:
@{
ViewBag.Title = "Login";
}
<h2>会员登录</h2>
@using (Html.BeginForm())
{
<label>用户名:</label><input type="text" name="name" /><br />
<label>密码:</label><input type="password" name="password" /><br />
<input type="submit" value="登录" />
}
当没有登录直接访问Member/Index时,会跳转到Login。当输入正确的用户名密码登录时,会跳转到Index页面。


完整的MemberController代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace Zhong.Web.Controllers
{
[Authentication]
public class MemberController : Controller
{
// GET: Member
public ActionResult Index()
{
return View();
}
[AllowAnonymous]
public ActionResult Login()
{
return View();
}
[AllowAnonymous]
[HttpPost]
public ActionResult Login(string name,string password)
{
//这里为了简单演示一下登录的判断,主要是验证AuthenticationAttribute的作用,实际的运用中可能要查询数据库、
//密码判断需要加密处理等
if (name == "test" && password == "")
{
HttpCookie cookie = new HttpCookie("Member");
cookie.Values["name"] = "test";
cookie.Values["pass"] = "";
Response.Cookies.Add(cookie);
return RedirectToAction("Index");
}
return View();
}
}
}
特别说明:由于控制器使用了Authentication特性,所以请求其下的所有Action都要先通过授权/登录 验证,Login是登录页面,访问这个页面一般是没有登录的状态,所以需要允许匿名访问,于是加了[AllowAnonymous]

上面的AuthorizationAttribute的另一种写法是继承FilterAttribute 并实现接口IAuthorizationFilter,方式与系统的AuthorizeAttribute类似,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace Zhong.Web
{
public class TestAttribute : FilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
//throw new NotImplementedException();
//TODO: 此处写需要实现登录验证的代码 }
}
}
ASP.NET MVC使用AuthenticationAttribute验证登录的更多相关文章
- ASP.NET MVC下判断用户登录和授权的方法
日常开发的绝大多数系统中,都涉及到管理用户的登录和授权问题.登录功能(Authentication),针对于所有用户都开放:而授权(Authorization),则对于某种用户角色才开放. 在asp. ...
- ASP.NET MVC下判断用户登录和授权状态方法
在我们日常开发的绝大多数系统中,都涉及到管理用户的登录和授权问题.登录功能(Authentication),针对于所有用户都开放:而授权(Authorization),则对于某种用户角色才开放. 在a ...
- ASP.NET MVC项目演练:用户登录
ASP.NET MVC 基础入门 http://www.cnblogs.com/liunlls/p/aspnetmvc_gettingstarted.html 设置默认启动页面 public clas ...
- Easyui + asp.net MVC 系列教程 完成登录
Easyui + asp.net MVC 系列教程 第09-17 节 完成登录 高清录制 前面八节 在这里 Easyui + asp.net mvc + sqlite 开发教程(录屏)适合入门 在接下 ...
- ASP.NET MVC Cookie 身份验证
1 创建一个ASP.NET MVC 项目 添加一个 AccountController 类. public class AccountController : Controller { [HttpGe ...
- ASP.NET MVC的客户端验证:jQuery验证在Model验证中的实现
在简单了解了Unobtrusive JavaScript形式的验证在jQuery中的编程方式之后,我们来介绍ASP.NET MVC是如何利用它实现客户端验证的.服务端验证最终实现在相应的ModelVa ...
- ASP.NET MVC的客户端验证:jQuery的验证
之前我们一直讨论的Model验证仅限于服务端验证,即在Web服务器根据相应的规则对请求数据实施验证.如果我们能够在客户端(浏览器)对用户输入的数据先进行验证,这样会减少针对服务器请求的频率,从而缓解W ...
- 【MVC】ASP.NET MVC之数据验证
前端传到后端数据的不可信任性,DRY("Don't Repeat Yourself") 设计原则.MVC3.0出了后端数据验证特性,鼓励你只定义一次功能或行为,然后在应用程序中各处 ...
- ASP.NET MVC 自动模型验证
经常看到这个代码 在controller 中写入验证模型,每个需要验证的action 都写-.. ,就问你烦不烦~ 可以利用 ASP.NET MVC 的 action 拦截机制 自动处理. 1 新建验 ...
随机推荐
- 为android游戏开发-准备的地图编辑器-初步刷地图
采用多文理混合,单页面支持8张文理进行刷绘
- linux内核移植过程问题总结
移植内核:2.6.30.4内核根目录下的.config为当前配置内核的且已经配置好的内核配置.make zImage以此为依据配置内核的过程:cd linux-2.6.30.4(进入Linux根目录) ...
- Maven 学习笔记(二)
项目最近开始使用maven去管理项目啦,说真的对于maven是一窍不通啊,今天和同事在回家的路上聊天的时候同事说他去第一家公司面试的时候人家问他 maven 怎么打包,当时我就懵逼了,因为我也不知道啊 ...
- 基于TrueLicense实现产品License验证功能
受朋友所托,需要给产品加上License验证功能,进行试用期授权,在试用期过后,产品不再可用. 通过研究调查,可以利用Truelicense开源框架实现,下面分享一下如何利用Truelicense实现 ...
- windows10下找回照片查看器的方法(仅作记录)
Windows Registry Editor Version 5.00 ; Change Extension's File Type [HKEY_CURRENT_USER\Software\Clas ...
- 数据库应用(Mysql、Mongodb、Redis、Memcached、CouchDB、Cassandra)
目前,主流数据库包括关系型(SQL)和非关系型(NoSQL)两种. 关系数据库是建立在关系模型基础上的数据库,借助于集合代数等数学概念和方法来处理数据库中的数据,支持复杂的事物处理和结构化查询.代表实 ...
- 解决php中文乱码
在文件的第一行,加入下面这一句: header("Content-Type: text/html; charset=utf8"); 然后在把文件以utf-8的格式保存起来就行了
- 值得收藏的TCP套接口编程文章
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由jackieluo发表于云+社区专栏 TCP客户端-服务器典型事件 下图是TCP客户端与服务器之间交互的一系列典型事件时间表: 首先启 ...
- .Net Core使用 MiniProfiler 进行性能分析(转)
转自:http://www.cnblogs.com/ideacore/p/9505425.html 官方文档: https://miniprofiler.com/dotnet/AspDotNetCor ...
- 使用Quartz.net来执行定时任务
Quartz.net使用方法:http://www.cnblogs.com/lizichao1991/p/5707604.html 最近,项目中需要执行一个计划任务,组长就让我了解一下Quartz.n ...