权限验证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系统的基础功能,一个 ...
随机推荐
- 【题解】CF1103D Professional layer
[题解]CF1103DProfessional layer 神题做前先\(orzyyb\) 一个很好的性质(之前也见过但是没有想到的) zhengchu \(gcd\le 10^{12}\) 所以不同 ...
- 十分钟git-服务器搭建ssh登陆
QQ820688215 微信公众号: 1首先,创建一个操作系统用户 git,并为其建立一个 .ssh 目录. $ sudo adduser git $ su git $ cd $ mkdir .ssh ...
- 序列化组件(get/put/delete接口设计),视图优化组件
一 . 知识点回顾 1 . 混入类 , 多继承 class Animal(object): def eat(self): print("Eat") def walk(self): ...
- css浏览器兼容问题集锦
表单按钮用input type=submit和a链接两者表现不一致的问题 表单的输入框.文本.验证码图片没有对齐 IE6/7中margin失效 IE6中margin双边距 1.问题: 表单按钮用inp ...
- Mysql远程登陆错误:ERROR 2003
不能远程登陆Mysql,错误:ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.0.114' (10060).原因是电脑防火墙 ...
- 64位 Windows 用了 32位编译平台 编译不过 MySQL API
发生在一周前的事情了,当时想感受下 MySQL C API ,就写了几个小例子.虽然是在 Windows(我的工作电脑是 64位 Windows) 上面,但是不想用 VS ,只想用文本软件写好代码后用 ...
- Windows下使用vim编写代码,使用nmake编译代码,使用vs来调试代码
1.编写代码 2.编写Makefile,如果要调试, 2.1.需要在编译的时候加上/Zi ( Generates complete debugging information),编译由cl.exe来完 ...
- 小心transform
张老师总结的,感谢! <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- PHP留言小练习
实现功能: 留言.搜索.编辑.删除.详情页.时间.点击量 页面划分: index.html(留言列表页) add.html(留言页) edit.php(编辑页) del.php(删除页) view.p ...
- k8s-应用快速入门(ma)
[root@k8s-master ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1 deplo ...