mvc5权限管理(简单登录):ActionFilterAttribute
效果图:



1.控制器
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(User user)
{
if( LoginBLL.CheckLogin(ref user)) {
MyCookie.Write(user);
return Content("loging is ok <a href='/jq/List'>查看</a>");
}
else
{ return Content("login is error:");
}
} public ActionResult List() {
User user = MyCookie.ReadCurCookie();
string con = "card = " + user.card + " pwd = " + user.pwd + " lv =" + user.lv +"<p/>" ;
con += "<a href='write'>写</a><p/>";
con += "<a href='read'>读</a><p/>";
return Content(con);
} [MyValidate(PowType = )] //权限管理 权限为1
public ActionResult read() {
return Content("you can read");
}
[MyValidate(PowType = )] //权限管理 权限为2
public ActionResult write() {
return Content("you can write");
} public ActionResult error()
{
return Content("权限不够哦");
}
这个里面的模型类是:
public class User
{
public string card { get; set; }
public string pwd { get; set; }
public int lv { get; set; }
}
2.封装类
设置权限验证的类:
public class MyValidate : ActionFilterAttribute
{
public string Power;
public int PowType;
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
try
{
// user is cooke infor
User mod = MyCookie.ReadCurCookie();
// check login status
if (!LoginBLL.CheckLogin(ref mod))
{
filterContext.Result = new RedirectResult("/JQ/index");
}
// Pow type is control on [MyValidate(PowType = ?)] set values
if ( mod.lv != PowType)
{
filterContext.Result = new RedirectResult("/jq/error");
//this return route(
//new RedirectToRouteResult("Default", new RouteValueDictionary(new { controller = "Home", action = "Default" }));
}
}
catch
{
filterContext.Result = new RedirectResult("/JQ/Login");
}
} //当方法执行完毕
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
}
}
读写cookie
public class MyCookie
{
public static void Write(User user)
{
FormsAuthentication.SetAuthCookie(user.card, true, FormsAuthentication.FormsCookiePath);
//把用户对象保存在票据里
FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(, user.card, DateTime.Now, DateTime.Now.AddTicks(FormsAuthentication.Timeout.Ticks), false, MyJosnHelper.ToJson(user));
//MyJosnHelper.ToJson(user) 存储在票据的 UserData 处,这个地方怎么操作由你决定
//加密票据
string hashTicket = FormsAuthentication.Encrypt(Ticket);
HttpCookie userCookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket);
System.Web.HttpContext.Current.Response.Cookies.Add(userCookie);
} public static User ReadCurCookie()
{
try
{
var cookie = System.Web.HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]; //的到Cookie
if (cookie == null)
return null;
var ticket = FormsAuthentication.Decrypt(cookie.Value); //解密票据
string str = ticket.UserData;
return MyJosnHelper.JsonDeserialize<User>(str);
}
catch
{
return null;
}
} public static void ReMoveCookie()
{
FormsAuthentication.SignOut();
}
}
模型json互转
public class MyJosnHelper
{
public static T JsonDeserialize<T>(string jsonString)
{
return JsonConvert.DeserializeObject<T>(jsonString);
} public static string ToJson(object obj)
{
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
return JsonConvert.SerializeObject(obj);
} }
2016.5.12 使用部分视图显示登陆效果

@using Microsoft.AspNet.Identity
@using ssm.Models
@using SSM.comm @if ( MyCookie.Read() != null )
{
<ul class="nav navbar-nav navbar-right">
<li>
@Html.ActionLink("你好," + MyCookie.Read().Adm_Name + "!", "Infor", "grzx", routeValues: null, htmlAttributes: new { title = "Manage" })
</li>
<li><a href="/home/out">注销</a></li>
</ul>
}
else
{
<ul class="nav navbar-nav navbar-right">
<li>@Html.ActionLink("登录", "Login", "Home", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
</ul>
}
扩展文档:
http://www.cnblogs.com/zxjyuan/archive/2009/08/21/1551196.html FormsAuthenticationTicket对象
代码包:效果在控制器 jq里面
http://pan.baidu.com/s/1kUx7I31
mvc5权限管理(简单登录):ActionFilterAttribute的更多相关文章
- Shiro 整合SpringMVC 并实现权限管理,登录和注销
Shiro 整合SpringMVC 并且实现权限管理,登录和注销 Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring S ...
- Shiro 整合SpringMVC 并且实现权限管理,登录和注销
Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Security,可能没有Spring Security做的功能强大 ...
- linux100day(day7)--用户管理和权限管理简单介绍
系统基础 计算机的三大部件 CPU 内存 IO 总线 一般使用system call和api来调用硬件 一些基础命令, pwd 查看当前路径 cal 计算器 clock 时钟 hwclock 显示与设 ...
- 人人中的 shiro权限管理 简单说明
maven shiro包的引用路径 :C:\Users\yanfazhongxin\.m2\repository\org\apache\shiro\shiro-core\1.3.2\shiro-co ...
- 六、EnterpriseFrameWork框架基础功能之权限管理
回<[开源]EnterpriseFrameWork框架系列文章索引> 从本章开始进入框架的第二块内容“EnterpriseFrameWork框架的基础功能”,包括:权限管理.字典数据管理. ...
- EASYUI+MVC4通用权限管理平台
通用权限案例平台在经过几年的实际项目使用,并取得了不错的用户好评.在平台开发完成后,特抽空总结一下平台知识,请各位在以后的时间里,关注博客的更新. 1.EASYUI+MVC4通用权限管理平台--前言 ...
- (转)EASYUI+MVC4通用权限管理平台
原文地址:http://www.cnblogs.com/hn731/archive/2013/07/15/3190947.html 通用权限案例平台在经过几年的实际项目使用,并取得了不错的用户好评.在 ...
- Asp.Net MVC+BootStrap+EF6.0实现简单的用户角色权限管理10
今天把用户的菜单显示和页面的按钮显示都做好了,下面先来个效果图 接下来说下我实现的方法: 首先我在每个方法前面都加了这个属性, /// <summary> /// 表示当前Action请求 ...
- mvc5+ef6+Bootstrap 项目心得--身份验证和权限管理
1.mvc5+ef6+Bootstrap 项目心得--创立之初 2.mvc5+ef6+Bootstrap 项目心得--身份验证和权限管理 3.mvc5+ef6+Bootstrap 项目心得--WebG ...
随机推荐
- svn(http)
对于有些公司的运维 他确实很垃圾 所以 你一定要谨记这一点!!!
- SDWebImage原理及使用
这个类库提供一个UIImageView类别以支持加载来自网络的远程图片.具有缓存管理,异步下载,同一个URL下载次数控制和优化等特征. SDWebImage加载图片的流程 入口 setImageWit ...
- 新萝卜家园GHOST WIN7系统32,64位极速装机特别版
系统来自:系统妈:http://www.xitongma.com 新萝卜家园GHOST win7系统32位极速装机特别版 V2016年3月 系统概述 新萝卜家园ghost win7系统32位极速装机特 ...
- 【代码笔记】iOS-点击任何处,显示出红色的UIView
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> //头文件 #import "MoreView. ...
- JQuery制作简单的网页导航特效
使用JQuery中hover()方法,使其根据鼠标的移动简单的改变背景颜色; hover();用于模拟鼠标指针悬停事件,当鼠标指针移动到元素上时,会触发指定的第一个函数,当鼠标指针移除这个元素时,会触 ...
- mysql replace 替换函数
UPDATE ecm_goods SET description = replace(description,'原字符串', '替换为') WHERE 条件;
- 1.7 基础知识——GP2.6 配置管理(CM)
摘要: 某某工作产品要进行配置管理,并不意味非要进行很重型的配置管理,根据实际需要采取合适的方式就可以了. 正文: GP2.6 Place designed work products of XXX ...
- js 获取当前系统时间
Js获取当前日期时间及其它操作 var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年 ...
- openstack新建虚机、网络、路由时候对应的ovs网桥的变化
[root@wb5 ~]# [root@wb5 ~]# [root@wb5 ~]# [root@wb5 ~]# [root@wb5 ~]# [root@wb5 ~]# [root@wb5 ~]# [r ...
- yii2开发后记
h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...