效果图:

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. Android Tips: 在给drawable中添加图片资源时,文件名必须全小写

    在给drawable中添加图片资源时,文件名必须全小写

  2. Problem with WinRM on Exchange 2013 Management Shell and Exchange Toolbox on a new exchange 2013 with CAFE and BE on single server installation

    While deploying MS Exchange 2013 I experienced issues with accessing the Exchange Management Shell a ...

  3. Xcode中的常用快捷键

    新建项目     com + shift +N 新建文件    com  + N 偏好设置  通用   com + , 跳到指定行 com + L 当前行加断点    com + \ 移动编辑区最上方 ...

  4. 查看特定View的默认属性值

    当我在分析focus.touch事件处理代码时发现,有些属性对代码的逻辑有非常重要的影响,比如clickable.focusable 这些属性.这时我们自然而然的想到,那么这些属性的默认值是什么呢?在 ...

  5. Nutz中那些好用的工具类

    Nutz 是国产的精品开源框架,它全无依赖,只以1兆多的身材,就可以实现SSH全部功能的90%以上.内容主要涵盖了:Ioc.Aop.MVC.Dao.Json等WEB开发的方方面面.  它不仅轻巧,而且 ...

  6. 1.7 基础知识——GP2.6 配置管理(CM)

    摘要: 某某工作产品要进行配置管理,并不意味非要进行很重型的配置管理,根据实际需要采取合适的方式就可以了. 正文: GP2.6 Place designed work products of XXX ...

  7. CSS3 background-size属性

    请复制粘贴,图片请自带 <!DOCTYPE html > <html > <head> <meta charset="utf-8"> ...

  8. JAVA中的注解小结

    以下内容参考java编程思想-4,jdk版本为jdk5.0,有点老-_-||| 什么是注解 JAVA SE5引入,也称元数据,可以直接添加到代码中,用来完整描述程序所需的信息,而这些信息是无法用Jav ...

  9. C# 获取 新浪微博登录之后的 完整的Cookie

    程序说明: 1.此项目 包含两个项目, 一个 Winform WinGetMocroblogCookie 用于手动 登录 新浪微博 其中涉及到的技术有: 使用webbrowser 获取HttpOnly ...

  10. Mongodb源代码阅读笔记:Journal机制

    Mongodb源代码阅读笔记:Journal机制 Mongodb源代码阅读笔记:Journal机制 涉及的文件 一些说明 PREPLOGBUFFER WRITETOJOURNAL WRITETODAT ...