权限验证MVC
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的更多相关文章
- ASP.NET MVC View 和 Web API 的基本权限验证
ASP.NET MVC 5.0已经发布一段时间了,适应了一段时间,准备把原来的MVC项目重构了一遍,先把基本权限验证这块记录一下. 环境:Windows 7 Professional SP1 + Mi ...
- MVc Forms Membership rolemanage 角色权限验证管理
Forms 登录验证Membership 权限验证rolemanage 角色管理 以往的项目中只有单纯的Forms 验证今天想把这三个结合到mvc 中发现要导入aspnet_ 相关表,但是有个问题验 ...
- Asp.net Mvc 身份验证、异常处理、权限验证(拦截器)实现代码
本问主要介绍asp.net的身份验证机制及asp.net MVC拦截器在项目中的运用.现在让我们来模拟一个简单的流程:用户登录>权限验证>异常处理 1.用户登录 验证用户是否登录成功步骤直 ...
- ASP.NET MVC权限验证 封装类
写该权限类主要目地 为了让权限配置更加的灵活,可以根据SQL.json.或者XML的方式来动态进行页面的访问控制,以及没有权限的相关跳转. 使用步骤 1.要建一个全局过滤器 //受权过滤器 publi ...
- Spring MVC 使用拦截器优雅地实现权限验证功能
在上一篇 SpringAOP 实现功能权限校验功能 中虽然用AOP通过抛异常,请求转发等勉强地实现了权限验证功能,但感觉不是那么完美,应该用拦截器来实现才是最佳的,因为拦截器就是用来拦截请求的,在请求 ...
- MVC 自定义AuthorizeAttribute 实现权限验证
MVC内置的AuthorizeFilter先于Action/Result过滤器执行,为网站权限验证提供了很好的一套验证机制. 通过自定义的AuthorizeAttribute可以实现对用户权限的验证. ...
- NET MVC权限验证
ASP.NET MVC权限验证 封装类 写该权限类主要目地 为了让权限配置更加的灵活,可以根据SQL.json.或者XML的方式来动态进行页面的访问控制,以及没有权限的相关跳转. 使用步骤 1.要建一 ...
- C# MVC权限验证
前言 之前一直没怎么接触过权限验证这块,刚好公司老平台改版,就有了这篇权限验证.此篇文章大致讲解下 精确到按钮级别的验证如何实现.以及权限验证设计的参考思路(菜鸟一枚,大神勿喷). 在开发大项目的时候 ...
- Web用户的身份验证及WebApi权限验证流程的设计和实现 asp.net mvc AllowAnonymous 不起作用, asp.net mvc 匿名访问
原文地址: https://blog.csdn.net/zjlovety/article/details/17095627 前言:Web 用户的身份验证,及页面操作权限验证是B/S系统的基础功能,一个 ...
随机推荐
- 镜像回源主要用于无缝迁移数据到OSS,即服务已经在自己建立的源站或者在其他云产品上运行,需要迁移到OSS上,但是又不能停止服务,此时可利用镜像回写功能实现。
管理回源设置_管理文件_开发指南_对象存储 OSS-阿里云 https://help.aliyun.com/document_detail/31865.html 通过回源设置,对于获取数据的请求以多种 ...
- There are two different types of export, named and default
export - JavaScript | MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statemen ...
- python cookbook第三版学习笔记四:文本以及字符串令牌解析
文本处理: 假设你存在一个目录,下面存在各种形式的文件,有txt,csv等等.如果你只想找到其中一种或多种格式的文件并打开该如何办呢.首先肯定是要找到满足条件的文件,然后进行路径合并在一一打开. pa ...
- Spring IOC 容器源码分析(转)
原文地址 Spring 最重要的概念是 IOC 和 AOP,本篇文章其实就是要带领大家来分析下 Spring 的 IOC 容器.既然大家平时都要用到 Spring,怎么可以不好好了解 Spring 呢 ...
- gon
gem 'gon' application.html 中添加 =include_gon action中 gon.activities = @activities js中 gon.activities
- Ruby 打印
puts: 输出内容自动换行,转义后再输出(转义符),可传递多个参数puts("this is ge num=",a,"this is b=",b) pri ...
- CSS3定时提示动画特效
在线演示 本地下载
- <JAVA8新增内容>关于集合的操作(Collection/Iterator/Stream)
因为下文频繁使用lambda表达式,关于Java中的lambda表达式内容请见: http://www.cnblogs.com/guguli/p/4394676.html 一.使用增强的Iterato ...
- poj 1469 COURSES (二分图模板应用 【*模板】 )
COURSES Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18454 Accepted: 7275 Descript ...
- SDUT OJ类型转换函数的应用
题目描述 处理一个复数与一个double数相加的运算,结果存放在一个double型变量d1中,输出d1的值.定义Complex(复数)类,在成员函数中包含重载类型转换运算符:operator doub ...