http://www.jb51.net/article/20147.htm  引用

<authentication mode="Forms"><!--权限受到阻碍将会跳转到这个页面,不写找不到的时候则报404-->
<forms loginUrl="~/ExcelEport/Login" timeout="2880" />
</authentication>

// [Authorize(Roles = "Admin")]//只有通过用户才可以访问该方法
public ActionResult likJinbulai()//你要设置权限的页面
{
//判断通过身份验证的用户是否有权限访问本页面
FormsIdentity id = (FormsIdentity)HttpContext.User.Identity;
//判断通过身份验证的用户是否是Admin角色
if (id.Ticket.UserData.Contains("Admin"))
{
return View();
//跳转到访问权限不够的错误提示页面
}

else
{
ViewBag.Authorize = false;
return View("youjinbulai");
//从哪里来回哪里去,在湖区的页面添加以下代码
//@if (@ViewBag.Authorize!=null)
//{
// if (!(@ViewBag.Authorize) )
// {
// <script type="text/javascript">
// $(function () {
// alert("您没有权限");
// });
// </script>
// }
//}
}

}

///登陆用户处理Action

public ActionResult LoginHandler(string userName, string userPwd,string role)
{
//mvc
////该用户通过验证后,会得到数据库字段Roles
////验证成功直接写入客户端cookie票据
//FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
//1,
//userName,//用户登陆进来的用户名
//DateTime.Now,
//DateTime.Now.AddMinutes(20),
//true,//是否存在持久,存储在客户端
//role//登陆用户的角色写入登陆用户的角色
//);
////加密身份验证票据
//string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
////把准备好的cookie加入到响应流中
//System.Web.HttpCookie authCookie = new System.Web.HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
//authCookie.Expires = authTicket.Expiration;//票据过期时间
////把准备好的cookie加入到响应流中
//System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);

//web
//Forms身份验证初始化
FormsAuthentication.Initialize();
//验证用户输入并得到登录用户,txtName是用户名称,txtPassword是登录密码
//UserModel um = ValidUser(txtName.Text.Trim(), txtPassword.Text.Trim());

//创建身份验证票据
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
userName,
DateTime.Now,
DateTime.Now.AddMinutes(30),
true,
role,//用户所属的角色字符串
FormsAuthentication.FormsCookiePath);
//加密身份验证票据
string hash = FormsAuthentication.Encrypt(ticket);
//创建要发送到客户端的cookie
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
if (ticket.IsPersistent)
{
cookie.Expires = ticket.Expiration;
}
//把准备好的cookie加入到响应流中
Response.Cookies.Add(cookie);

//转发到请求的页面
//Response.Redirect(FormsAuthentication.GetRedirectUrl(userName, false));
return View("youjinbulai");
}

//注销票据 
public ActionResult ClearTicket()
{
//注销票据
FormsAuthentication.SignOut();
string script = "alert('您已经安全退出了!');";
return JavaScript(script);
}

//以下写在Global.asax

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
//mvc
//HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
//if (authCookie == null || authCookie.Value == "")
//{
// return;
//}
//FormsAuthenticationTicket authTicket = null;
//try
//{
// authTicket = FormsAuthentication.Decrypt(authCookie.Value);
//}
//catch
//{
// return;
//}
//string[] roles = authTicket.UserData.Split(new char[] { ';' });
//if (Context.User != null)
//{
// Context.User = new System.Security.Principal.GenericPrincipal(Context.User.Identity, roles);
//}
//webform
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;

string userData = ticket.UserData;
string[] roles = userData.Split(',');
//重建HttpContext.Current.User,加入用户拥有的角色数组
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles);
}
}
}
}

权限验证MVC的更多相关文章

  1. ASP.NET MVC View 和 Web API 的基本权限验证

    ASP.NET MVC 5.0已经发布一段时间了,适应了一段时间,准备把原来的MVC项目重构了一遍,先把基本权限验证这块记录一下. 环境:Windows 7 Professional SP1 + Mi ...

  2. MVc Forms Membership rolemanage 角色权限验证管理

    Forms  登录验证Membership 权限验证rolemanage 角色管理 以往的项目中只有单纯的Forms 验证今天想把这三个结合到mvc 中发现要导入aspnet_ 相关表,但是有个问题验 ...

  3. Asp.net Mvc 身份验证、异常处理、权限验证(拦截器)实现代码

    本问主要介绍asp.net的身份验证机制及asp.net MVC拦截器在项目中的运用.现在让我们来模拟一个简单的流程:用户登录>权限验证>异常处理 1.用户登录 验证用户是否登录成功步骤直 ...

  4. ASP.NET MVC权限验证 封装类

    写该权限类主要目地 为了让权限配置更加的灵活,可以根据SQL.json.或者XML的方式来动态进行页面的访问控制,以及没有权限的相关跳转. 使用步骤 1.要建一个全局过滤器 //受权过滤器 publi ...

  5. Spring MVC 使用拦截器优雅地实现权限验证功能

    在上一篇 SpringAOP 实现功能权限校验功能 中虽然用AOP通过抛异常,请求转发等勉强地实现了权限验证功能,但感觉不是那么完美,应该用拦截器来实现才是最佳的,因为拦截器就是用来拦截请求的,在请求 ...

  6. MVC 自定义AuthorizeAttribute 实现权限验证

    MVC内置的AuthorizeFilter先于Action/Result过滤器执行,为网站权限验证提供了很好的一套验证机制. 通过自定义的AuthorizeAttribute可以实现对用户权限的验证. ...

  7. NET MVC权限验证

    ASP.NET MVC权限验证 封装类 写该权限类主要目地 为了让权限配置更加的灵活,可以根据SQL.json.或者XML的方式来动态进行页面的访问控制,以及没有权限的相关跳转. 使用步骤 1.要建一 ...

  8. C# MVC权限验证

    前言 之前一直没怎么接触过权限验证这块,刚好公司老平台改版,就有了这篇权限验证.此篇文章大致讲解下 精确到按钮级别的验证如何实现.以及权限验证设计的参考思路(菜鸟一枚,大神勿喷). 在开发大项目的时候 ...

  9. Web用户的身份验证及WebApi权限验证流程的设计和实现 asp.net mvc AllowAnonymous 不起作用, asp.net mvc 匿名访问

    原文地址: https://blog.csdn.net/zjlovety/article/details/17095627 前言:Web 用户的身份验证,及页面操作权限验证是B/S系统的基础功能,一个 ...

随机推荐

  1. runsv

    runsv(8) manual page http://smarden.org/runit/runsv.8.html Name runsv - starts and monitors a servic ...

  2. java中byte转string的方法有哪些?

    1.第一种 byte b = 1; String valueOf = String.valueOf(b) 2.第二种 byte b = 1; String st = Byte.toString(b); ...

  3. Linux下Mysql数据库忘记root

    系统环境:Red Hat Enterprise Linux Server 6 1.停止mysqld服务 [root@Server huage]# service mysqld stop 2.以跳过授权 ...

  4. Java for LeetCode 115 Distinct Subsequences【HARD】

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  5. 顽石系列:Java技术面试

    顽石系列:Java技术面试 JDBC相关 1.Statement与PreparedStatement的区 别,什什么是SQL注⼊入,如何防⽌止SQL注⼊? PreparedStatement支持动态设 ...

  6. IOS 工程所支持的版本 设置

    如何设置 Base SDK 和 iOS Deployment Target ? http://leopard168.blog.163.com/blog/static/16847184420116159 ...

  7. iPad actionsjeet

    在iphone和ipad上使用UIActionShee控件t的效果会不一样,在苹果的官方文档中有相关说明: 在ipad上使用UIActionSheet控件改控件不再从底部弹出,而是从屏幕中间弹出与UI ...

  8. vs2010 windows service 项目不能引用类库项目

    在一个windows 服务项目A中,引用了另外一个项目B,可以使用自动完成,引用其他项目中的类,按理,可以自动提示了,应该就是没问题了,但编译时却提示"未能找到类型或命名空间名称" ...

  9. RQNOJ 95 多多看DVD(加强版):01背包

    题目链接:https://www.rqnoj.cn/problem/95 题意: 叔叔要陪多多看动画片. 有n张DVD可以买,第i张碟的打分为w[i],播放时间为t[i]. 爷爷规定他们只能在一定的时 ...

  10. html5--2.10综合实例2-移动端页面练习

    html5--2.10综合实例2-移动端页面练习 学习要点 通过一个简单的移动手机页面,复习学过的内容 手机网页的测试 手机布局的屏幕设定 手机网页的测试方法 直接在手机上测试,比较麻烦,效果好 电脑 ...