效果图:

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的更多相关文章

  1. Shiro 整合SpringMVC 并实现权限管理,登录和注销

    Shiro 整合SpringMVC 并且实现权限管理,登录和注销 Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring S ...

  2. Shiro 整合SpringMVC 并且实现权限管理,登录和注销

    Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Security,可能没有Spring Security做的功能强大 ...

  3. linux100day(day7)--用户管理和权限管理简单介绍

    系统基础 计算机的三大部件 CPU 内存 IO 总线 一般使用system call和api来调用硬件 一些基础命令, pwd 查看当前路径 cal 计算器 clock 时钟 hwclock 显示与设 ...

  4. 人人中的 shiro权限管理 简单说明

    maven  shiro包的引用路径 :C:\Users\yanfazhongxin\.m2\repository\org\apache\shiro\shiro-core\1.3.2\shiro-co ...

  5. 六、EnterpriseFrameWork框架基础功能之权限管理

    回<[开源]EnterpriseFrameWork框架系列文章索引> 从本章开始进入框架的第二块内容“EnterpriseFrameWork框架的基础功能”,包括:权限管理.字典数据管理. ...

  6. EASYUI+MVC4通用权限管理平台

    通用权限案例平台在经过几年的实际项目使用,并取得了不错的用户好评.在平台开发完成后,特抽空总结一下平台知识,请各位在以后的时间里,关注博客的更新. 1.EASYUI+MVC4通用权限管理平台--前言 ...

  7. (转)EASYUI+MVC4通用权限管理平台

    原文地址:http://www.cnblogs.com/hn731/archive/2013/07/15/3190947.html 通用权限案例平台在经过几年的实际项目使用,并取得了不错的用户好评.在 ...

  8. Asp.Net MVC+BootStrap+EF6.0实现简单的用户角色权限管理10

    今天把用户的菜单显示和页面的按钮显示都做好了,下面先来个效果图 接下来说下我实现的方法: 首先我在每个方法前面都加了这个属性, /// <summary> /// 表示当前Action请求 ...

  9. mvc5+ef6+Bootstrap 项目心得--身份验证和权限管理

    1.mvc5+ef6+Bootstrap 项目心得--创立之初 2.mvc5+ef6+Bootstrap 项目心得--身份验证和权限管理 3.mvc5+ef6+Bootstrap 项目心得--WebG ...

随机推荐

  1. 在Sharepoint 2013中,使用JS判断当前用户是否在某个组里面

    使用Sharepoint客户端对象模型,判断当前用户是否在某个组里面. 在View 和 Edit List Item的时候使用,使用户编辑修改List Item的时候有权限的区分. 在Edit 页面加 ...

  2. Android优秀学习资料(高手博客

    任玉刚, 博客 : http://blog.csdn.net/singwhatiwanna, github : https://github.com/singwhatiwanna Trinea, 博客 ...

  3. sleep() 和 wait() 的区别

    好多面经上都出现了,有必要好好熟悉一下 区别: 1.wait() 可以指定时间,也可以不指定(等五分钟你进来,或者是不叫你一直等着):sleep()必须指定时间(不能一睡不起) 2.wait()是Ob ...

  4. 第二个activity

    Android四大组件 Activity BroadCastReceiver Service ContentProvider 创建第二个Activity 创建第2个Activity的过程 1. 创建c ...

  5. GIT和SVN之间的五个基本区别

    GIT不仅仅是个版本控制系统,它也是个内容管理系统(CMS),工作管理系统等.如果你是一个具有使用SVN背景的人,你需要做一定的思想转换,来适应GIT提供的一些概念和特征.所以,这篇文章的主要目的就是 ...

  6. CMMI能搭救企业吗?

    曾经有朋友跟我提起,他们公司突然有人关注我了,我自然很开心了,看来我名声在外了,哈哈!但我的朋友说,他是这样关注的:“他怎么还在那个公司?他们不是过了5级了吗?我们公司过了级后,EPG(Enginee ...

  7. 华为S5700S-52P-LI-AC千兆网管交换机web登录界面配置

    研究一下午,包装附的说明书根本就是错误的,通过技术售后和官方的文档结合,总算可以登录交换机的web管理界面. 首先需要使用通讯控制线缆(包装中附)连接电脑和交换机,一头接交换机的Console口,一头 ...

  8. spring + spring mvc + mybatis + react + reflux + webpack Web工程例子

    前言 最近写了个Java Web工程demo,使用maven构建: 后端使用spring + spring mvc + mybatis: 前端使用react + react-router+ webpa ...

  9. AS与.net的交互——详解UrlRequest

    在.net中我们知道有一个叫做WebHttpRequest的东西,用它我们可以实现各种网络偷窥,监控,采集和机器人,如果外加一 个模式识别,那真是吊爆了... 在as中我们也可以实现同样的功能,而且我 ...

  10. 烂泥:NFS做存储与KVM集成

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 以前有关NFS的文章,我们介绍的都是NFS的使用挂载等等.这篇文章我们介绍有关NFS作为存储使用. 既然本篇文章的主题是有关NFS的,我们还是先把NFS ...